RandomState =========== .. cpp:class:: numpy::RandomState Random number generation class. Example ------- .. code-block:: cpp #include 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 ------------ .. list-table:: :widths: 55 25 20 :header-rows: 1 * - Signature - Location - Example * - ``RandomState(uint64_tseed)`` - NP_RANDOM.H:37 - :ref:`View ` Indexing / Selection -------------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``std::mt19937_64 & get_generator()`` - std::mt19937_64 & - NP_RANDOM.H:46 - :ref:`View ` Random ------ .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``void seed(uint64_tseed)`` - void - NP_RANDOM.H:40 - :ref:`View ` Other Methods ------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``void set_state(const std::vector&state)`` - void - NP_RANDOM.H:57 - :ref:`View ` Code Examples ------------- The following examples are extracted from the test suite. .. _example-randomstate-randomstate-0: .. dropdown:: RandomState (np_test_1_all.cpp:25186) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 25176 :emphasize-lines: 11 } std::cout << " -> tests passed" << std::endl; } // ============================================================================ // RANDOMSTATE TESTS (LEGACY) // ============================================================================ void np_test_bitgen_randomstate() { std::cout << "========= RandomState (legacy) ======================="; numpy::random::RandomState rs(99999); // Test state management auto state = rs.get_state(); if (state.empty()) { std::cout << " [FAIL] : in np_test_bitgen_randomstate() : state empty"; throw std::runtime_error("np_test_bitgen_randomstate failed"); } .. _example-randomstate-get_generator-1: .. dropdown:: get_generator (np_test_1_all.cpp:25201) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 25191 :emphasize-lines: 11 auto state = rs.get_state(); if (state.empty()) { std::cout << " [FAIL] : in np_test_bitgen_randomstate() : state empty"; throw std::runtime_error("np_test_bitgen_randomstate failed"); } // Test set_state rs.set_state(state); // Test that we can use the generator auto& gen = rs.get_generator(); uint64_t val = gen(); if (val == 0 && gen() == 0 && gen() == 0) { std::cout << " [FAIL] : in np_test_bitgen_randomstate() : generator seems broken"; throw std::runtime_error("np_test_bitgen_randomstate failed"); } // Test seed rs.seed(12345); auto state2 = rs.get_state(); .. _example-randomstate-seed-2: .. dropdown:: seed (np_test_1_all.cpp:14339) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 14329 :emphasize-lines: 11 } // std::cout << "[OK] Generator integers method\n"; std::cout << " -> tests passed\n"; } void test_vsl_reproducibility() { std::cout << "========= test_vsl_reproducibility ===="; // Test seed reproducibility seed(42); auto arr1 = normal(0.0, 1.0, { {10} }); seed(42); auto arr2 = normal(0.0, 1.0, { {10} }); for (size_t i = 0; i < 10; ++i) { double val1 = arr1.getElementAt({ i }); double val2 = arr2.getElementAt({ i }); .. _example-randomstate-set_state-3: .. dropdown:: set_state (np_test_1_all.cpp:22363) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 22353 :emphasize-lines: 11 } // std::cout << "[OK] Random bytes\n"; // Test state functions std::string state = get_state(); if (!(!state.empty())) { std::string description = std::string("testRandomUtilityFunctions():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(!state.empty())"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } set_state(12345); // std::cout << "[OK] State functions\n"; std::cout << " -> tests passed\n"; } void testGeneratorAPI() { std::cout << "========= testGeneratorAPI ===="; // Test modern Generator API auto gen = create_generator(42);