PCG64 ===== .. cpp:class:: numpy::PCG64 Random number generation class. Example ------- .. code-block:: cpp #include using namespace numpy; // Use PCG64 PCG64 obj; // ... operations ... Constructors ------------ .. list-table:: :widths: 55 25 20 :header-rows: 1 * - Signature - Location - Example * - ``PCG64(uint64_tseed = 0x853c49e6748fea9bULL, uint64_tstream = 0)`` - NP_BITGEN.H:300 - Random ------ .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``void seed(uint64_ts)`` - void - NP_BITGEN.H:322 - :ref:`View ` Other Methods ------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``std::string name()`` - std::string - NP_BITGEN.H:347 - :ref:`View ` * - ``double next_double()`` - double - NP_BITGEN.H:316 - :ref:`View ` * - ``uint64_t next_uint64()`` - uint64_t - NP_BITGEN.H:310 - :ref:`View ` * - ``uint64_t pcg_output_rxs_m_xs(uint64_tstate)`` - uint64_t - NP_BITGEN.H:283 - * - ``void set_state(const std::vector&state)`` - void - NP_BITGEN.H:333 - :ref:`View ` * - ``void step()`` - void - NP_BITGEN.H:290 - :ref:`View ` Code Examples ------------- The following examples are extracted from the test suite. .. _example-pcg64-seed-0: .. 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-pcg64-name-1: .. dropdown:: name (np_test_1_all.cpp:24865) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 24855 :emphasize-lines: 11 uint64_t before_restore = rng.next_uint64(); rng.set_state(state); uint64_t after_restore = rng.next_uint64(); if (before_restore != after_restore) { std::cout << " [FAIL] : in np_test_bitgen_mt19937() : state restore failed"; throw std::runtime_error("np_test_bitgen_mt19937 failed: state"); } // Test name if (rng.name() != "MT19937") { std::cout << " [FAIL] : in np_test_bitgen_mt19937() : incorrect name"; throw std::runtime_error("np_test_bitgen_mt19937 failed: name"); } std::cout << " -> tests passed" << std::endl; } // ============================================================================ // PCG64 TESTS // ============================================================================ .. _example-pcg64-next_double-2: .. dropdown:: next_double (np_test_1_all.cpp:24837) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 24827 :emphasize-lines: 11 // ============================================================================ void np_test_bitgen_mt19937() { std::cout << "========= BitGenerator: MT19937 ======================="; // Test construction numpy::random::MT19937 rng(12345); // Test generation uint64_t val1 = rng.next_uint64(); double dval = rng.next_double(); if (dval < 0.0 || dval >= 1.0) { std::cout << " [FAIL] : in np_test_bitgen_mt19937() : next_double() out of range [" << dval << "]"; throw std::runtime_error("np_test_bitgen_mt19937 failed: double range"); } // Test seeding reproducibility numpy::random::MT19937 rng2(12345); uint64_t val2 = rng2.next_uint64(); .. _example-pcg64-next_uint64-3: .. dropdown:: next_uint64 (np_test_1_all.cpp:24836) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 24826 :emphasize-lines: 11 // MT19937 TESTS // ============================================================================ void np_test_bitgen_mt19937() { std::cout << "========= BitGenerator: MT19937 ======================="; // Test construction numpy::random::MT19937 rng(12345); // Test generation uint64_t val1 = rng.next_uint64(); double dval = rng.next_double(); if (dval < 0.0 || dval >= 1.0) { std::cout << " [FAIL] : in np_test_bitgen_mt19937() : next_double() out of range [" << dval << "]"; throw std::runtime_error("np_test_bitgen_mt19937 failed: double range"); } // Test seeding reproducibility numpy::random::MT19937 rng2(12345); uint64_t val2 = rng2.next_uint64(); .. _example-pcg64-set_state-4: .. 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); .. _example-pcg64-step-5: .. dropdown:: step (np_test_1_all.cpp:6929) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 6919 :emphasize-lines: 11 // Create array of dates datetime64 start("2024-01-01"); auto date_array = createDateTimeArray({5}, start); // std::cout << "Array of 5 dates initialized to 2024-01-01:" << std::endl; //date_array.printArray(); // Create date range datetime64 range_start("2024-01-01"); datetime64 range_end("2024-01-10"); timedelta64 step(1, DateTimeUnit::Day); auto date_range = createDateRange(range_start, range_end, step); // std::cout << "Date range from 2024-01-01 to 2024-01-10:" << std::endl; //date_range.printArray(); // Create timedelta array timedelta64 one_day(1, DateTimeUnit::Day); auto timedelta_array = createTimeDeltaArray({3}, one_day); // std::cout << "Array of 3 timedeltas (1 day each):" << std::endl;