Generator ========= .. cpp:class:: numpy::Generator 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 * - ``Generator(std::unique_ptrbg)`` - NP_BITGEN.H:662 - :ref:`View ` Random ------ .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``void seed(uint64_ts)`` - void - NP_BITGEN.H:778 - :ref:`View ` Other Methods ------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``BitGenerator & bit_generator()`` - BitGenerator & - NP_BITGEN.H:668 - * - ``void set_state(const std::vector&state)`` - void - NP_BITGEN.H:771 - :ref:`View ` Code Examples ------------- The following examples are extracted from the test suite. .. _example-generator-generator-0: .. dropdown:: Generator (np_test_1_all.cpp:25085) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 25075 :emphasize-lines: 11 } // ============================================================================ // GENERATOR TESTS // ============================================================================ void np_test_bitgen_generator() { std::cout << "========= Generator API ======================="; // Test with PCG64 auto rng = numpy::random::Generator( std::make_unique(54321) ); // Test uniform auto arr_uniform = rng.uniform(0.0, 1.0, { 10 }); if (arr_uniform.getSize() != 10) { std::cout << " [FAIL] : in np_test_bitgen_generator() : uniform size"; throw std::runtime_error("np_test_bitgen_generator failed"); } .. _example-generator-seed-1: .. 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-generator-set_state-2: .. 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);