MT19937#
-
class numpy::MT19937#
Random number generation class.
Example#
#include <numpy/np_ndarray.h>
using namespace numpy;
// Use MT19937
MT19937 obj;
// ... operations ...
Constructors#
Signature |
Location |
Example |
|---|---|---|
|
NP_BITGEN.H:181 |
Random#
Signature |
Return Type |
Location |
Example |
|---|---|---|---|
|
void |
NP_BITGEN.H:194 |
Other Methods#
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);