RandomState#

class numpy::RandomState#

Random number generation class.

Example#

#include <numpy/np_ndarray.h>
using namespace numpy;

// Create random generator
Generator gen(12345);  // seed

// Generate random values
auto uniform = gen.random({3, 3});
auto normal = gen.standard_normal({10});
auto integers = gen.integers(0, 100, {5});

Constructors#

Signature

Location

Example

RandomState(uint64_tseed)

NP_RANDOM.H:37

View

Indexing / Selection#

Signature

Return Type

Location

Example

std::mt19937_64 & get_generator()

std::mt19937_64 &

NP_RANDOM.H:46

View

Random#

Signature

Return Type

Location

Example

void seed(uint64_tseed)

void

NP_RANDOM.H:40

View

Other Methods#

Signature

Return Type

Location

Example

void set_state(const std::vector<uint64_t>&state)

void

NP_RANDOM.H:57

View

Code Examples#

The following examples are extracted from the test suite.

RandomState (np_test_1_all.cpp:25186)
25176      }
25177
25178      std::cout << " -> tests passed" << std::endl;
25179    }
25180
25181    // ============================================================================
25182    // RANDOMSTATE TESTS (LEGACY)
25183    // ============================================================================
25184
25185    void np_test_bitgen_randomstate() {
25186      std::cout << "========= RandomState (legacy) =======================";
25187
25188      numpy::random::RandomState rs(99999);
25189
25190      // Test state management
25191      auto state = rs.get_state();
25192      if (state.empty()) {
25193        std::cout << "  [FAIL] : in np_test_bitgen_randomstate() : state empty";
25194        throw std::runtime_error("np_test_bitgen_randomstate failed");
25195      }
get_generator (np_test_1_all.cpp:25201)
25191      auto state = rs.get_state();
25192      if (state.empty()) {
25193        std::cout << "  [FAIL] : in np_test_bitgen_randomstate() : state empty";
25194        throw std::runtime_error("np_test_bitgen_randomstate failed");
25195      }
25196
25197      // Test set_state
25198      rs.set_state(state);
25199
25200      // Test that we can use the generator
25201      auto& gen = rs.get_generator();
25202      uint64_t val = gen();
25203
25204      if (val == 0 && gen() == 0 && gen() == 0) {
25205        std::cout << "  [FAIL] : in np_test_bitgen_randomstate() : generator seems broken";
25206        throw std::runtime_error("np_test_bitgen_randomstate failed");
25207      }
25208
25209      // Test seed
25210      rs.seed(12345);
25211      auto state2 = rs.get_state();
seed (np_test_1_all.cpp:14339)
14329    }
14330    // std::cout << "[OK] Generator integers method\n";
14331
14332    std::cout << " -> tests passed\n";
14333  }
14334
14335  void test_vsl_reproducibility() {
14336    std::cout << "========= test_vsl_reproducibility ====";
14337
14338    // Test seed reproducibility
14339    seed(42);
14340    auto arr1 = normal<double>(0.0, 1.0, { {10} });
14341
14342    seed(42);
14343    auto arr2 = normal<double>(0.0, 1.0, { {10} });
14344
14345
14346
14347    for (size_t i = 0; i < 10; ++i) {
14348      double val1 = arr1.getElementAt({ i });
14349      double val2 = arr2.getElementAt({ i });
set_state (np_test_1_all.cpp:22363)
22353    }
22354    // std::cout << "[OK] Random bytes\n";
22355
22356    // Test state functions
22357    std::string state = get_state();
22358    if (!(!state.empty())) {
22359        std::string description = std::string("testRandomUtilityFunctions():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(!state.empty())";
22360        std::cout << std::string("[FAIL] ") + description << std::endl;
22361        throw std::runtime_error(description);
22362    }
22363    set_state(12345);
22364    // std::cout << "[OK] State functions\n";
22365
22366    std::cout << " -> tests passed\n";
22367  }
22368
22369  void testGeneratorAPI() {
22370    std::cout << "========= testGeneratorAPI ====";
22371
22372    // Test modern Generator API
22373    auto gen = create_generator(42);