PCG64#

class numpy::PCG64#

Random number generation class.

Example#

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

// Use PCG64
PCG64 obj;
// ... operations ...

Constructors#

Signature

Location

Example

PCG64(uint64_tseed = 0x853c49e6748fea9bULL, uint64_tstream = 0)

NP_BITGEN.H:300

Random#

Signature

Return Type

Location

Example

void seed(uint64_ts)

void

NP_BITGEN.H:322

View

Other Methods#

Signature

Return Type

Location

Example

std::string name()

std::string

NP_BITGEN.H:347

View

double next_double()

double

NP_BITGEN.H:316

View

uint64_t next_uint64()

uint64_t

NP_BITGEN.H:310

View

uint64_t pcg_output_rxs_m_xs(uint64_tstate)

uint64_t

NP_BITGEN.H:283

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

void

NP_BITGEN.H:333

View

void step()

void

NP_BITGEN.H:290

View

Code Examples#

The following examples are extracted from the test suite.

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 });
name (np_test_1_all.cpp:24865)
24855      uint64_t before_restore = rng.next_uint64();
24856      rng.set_state(state);
24857      uint64_t after_restore = rng.next_uint64();
24858
24859      if (before_restore != after_restore) {
24860        std::cout << "  [FAIL] : in np_test_bitgen_mt19937() : state restore failed";
24861        throw std::runtime_error("np_test_bitgen_mt19937 failed: state");
24862      }
24863
24864      // Test name
24865      if (rng.name() != "MT19937") {
24866        std::cout << "  [FAIL] : in np_test_bitgen_mt19937() : incorrect name";
24867        throw std::runtime_error("np_test_bitgen_mt19937 failed: name");
24868      }
24869
24870      std::cout << " -> tests passed" << std::endl;
24871    }
24872
24873    // ============================================================================
24874    // PCG64 TESTS
24875    // ============================================================================
next_double (np_test_1_all.cpp:24837)
24827    // ============================================================================
24828
24829    void np_test_bitgen_mt19937() {
24830      std::cout << "========= BitGenerator: MT19937 =======================";
24831
24832      // Test construction
24833      numpy::random::MT19937 rng(12345);
24834
24835      // Test generation
24836      uint64_t val1 = rng.next_uint64();
24837      double dval = rng.next_double();
24838
24839      if (dval < 0.0 || dval >= 1.0) {
24840        std::cout << "  [FAIL] : in np_test_bitgen_mt19937() : next_double() out of range [" << dval << "]";
24841        throw std::runtime_error("np_test_bitgen_mt19937 failed: double range");
24842      }
24843
24844      // Test seeding reproducibility
24845      numpy::random::MT19937 rng2(12345);
24846      uint64_t val2 = rng2.next_uint64();
next_uint64 (np_test_1_all.cpp:24836)
24826    // MT19937 TESTS
24827    // ============================================================================
24828
24829    void np_test_bitgen_mt19937() {
24830      std::cout << "========= BitGenerator: MT19937 =======================";
24831
24832      // Test construction
24833      numpy::random::MT19937 rng(12345);
24834
24835      // Test generation
24836      uint64_t val1 = rng.next_uint64();
24837      double dval = rng.next_double();
24838
24839      if (dval < 0.0 || dval >= 1.0) {
24840        std::cout << "  [FAIL] : in np_test_bitgen_mt19937() : next_double() out of range [" << dval << "]";
24841        throw std::runtime_error("np_test_bitgen_mt19937 failed: double range");
24842      }
24843
24844      // Test seeding reproducibility
24845      numpy::random::MT19937 rng2(12345);
24846      uint64_t val2 = rng2.next_uint64();
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);
step (np_test_1_all.cpp:6929)
6919    // Create array of dates
6920    datetime64 start("2024-01-01");
6921    auto date_array = createDateTimeArray({5}, start);
6922
6923    // std::cout << "Array of 5 dates initialized to 2024-01-01:" << std::endl;
6924    //date_array.printArray();
6925
6926    // Create date range
6927    datetime64 range_start("2024-01-01");
6928    datetime64 range_end("2024-01-10");
6929    timedelta64 step(1, DateTimeUnit::Day);
6930
6931    auto date_range = createDateRange(range_start, range_end, step);
6932    // std::cout << "Date range from 2024-01-01 to 2024-01-10:" << std::endl;
6933    //date_range.printArray();
6934
6935    // Create timedelta array
6936    timedelta64 one_day(1, DateTimeUnit::Day);
6937    auto timedelta_array = createTimeDeltaArray({3}, one_day);
6938
6939    // std::cout << "Array of 3 timedeltas (1 day each):" << std::endl;