Generator#

class numpy::Generator#

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

Generator(std::unique_ptr<BitGenerator>bg)

NP_BITGEN.H:662

View

Random#

Signature

Return Type

Location

Example

void seed(uint64_ts)

void

NP_BITGEN.H:778

View

Other Methods#

Signature

Return Type

Location

Example

BitGenerator & bit_generator()

BitGenerator &

NP_BITGEN.H:668

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

void

NP_BITGEN.H:771

View

Code Examples#

The following examples are extracted from the test suite.

Generator (np_test_1_all.cpp:25085)
25075    }
25076
25077    // ============================================================================
25078    // GENERATOR TESTS
25079    // ============================================================================
25080
25081    void np_test_bitgen_generator() {
25082      std::cout << "========= Generator API =======================";
25083
25084      // Test with PCG64
25085      auto rng = numpy::random::Generator(
25086        std::make_unique<numpy::random::PCG64>(54321)
25087      );
25088
25089      // Test uniform
25090      auto arr_uniform = rng.uniform<double>(0.0, 1.0, { 10 });
25091      if (arr_uniform.getSize() != 10) {
25092        std::cout << "  [FAIL] : in np_test_bitgen_generator() : uniform size";
25093        throw std::runtime_error("np_test_bitgen_generator failed");
25094      }
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);