cppcore
A small, portable C++ core library
Loading...
Searching...
No Matches
RandomGenerator.cpp
Go to the documentation of this file.
1/*-----------------------------------------------------------------------------------------------
2The MIT License (MIT)
3
4Copyright (c) 2014-2025 Kim Kulling
5
6Permission is hereby granted, free of charge, to any person obtaining a copy of
7this software and associated documentation files (the "Software"), to deal in
8the Software without restriction, including without limitation the rights to
9use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
10the Software, and to permit persons to whom the Software is furnished to do so,
11subject to the following conditions:
12
13The above copyright notice and this permission notice shall be included in all
14copies or substantial portions of the Software.
15
16THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
18FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
19COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
20IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22-----------------------------------------------------------------------------------------------*/
24
25#include <stdlib.h>
26#include <time.h>
27#include <cassert>
28
29namespace cppcore {
30
31static constexpr unsigned int N = 624;
32static constexpr unsigned int M = 397;
33
34static bool mersenne_twister_vector_init( unsigned int *seedPoints, size_t len ) {
35 if (seedPoints == nullptr) {
36 return false;
37 }
38
39 const unsigned int mult = 1812433253ul;
40 unsigned int seed = 5489ul;
41 for (size_t i = 0; i < len; ++i) {
42 seedPoints[ i ] = seed;
43 seed = mult * (seed ^ (seed >> 30)) + (static_cast<unsigned int>(i) + 1);
44 }
45
46 return true;
47}
48
49static bool mersenne_twister_vector_update(unsigned int* const p) {
50 if (p == nullptr) {
51 return false;
52 }
53
54 static const unsigned int A[ 2 ] = { 0, 0x9908B0DF };
55 unsigned int i=0;
56 for (; i < N - M; i++) {
57 p[i] = p[i + (M)] ^ (((p[i] & 0x80000000) | (p[i + 1] & 0x7FFFFFFF)) >> 1) ^ A[p[i + 1] & 1];
58 }
59 for (; i < N - 1; i++) {
60 p[i] = p[i + (M - N)] ^ (((p[i] & 0x80000000) | (p[i + 1] & 0x7FFFFFFF)) >> 1) ^ A[p[i + 1] & 1];
61 }
62 p[N - 1] = p[M - 1] ^ (((p[N - 1] & 0x80000000) | (p[0] & 0x7FFFFFFF)) >> 1) ^ A[p[0] & 1];
63
64 return true;
65}
66
67unsigned int mersenne_twister() {
68 // State-vector
69 static unsigned int vector[ N ];
70 // readout index
71 static int idx = N + 1;
72
73 bool ok = true;
74 if (static_cast<unsigned int>(idx) >= N) {
75 if (static_cast<unsigned int>(idx) > N) {
76 ok &= mersenne_twister_vector_init(vector, N);
77 }
79 idx = 0;
80 }
81 assert(ok);
82 unsigned int e = vector[ idx++ ];
83
84 // Tempering
85 e ^= (e >> 11);
86 e ^= (e << 7) & 0x9D2C5680;
87 e ^= (e << 15) & 0xEFC60000;
88 e ^= (e >> 18);
89
90 return e;
91}
92
94 mType( type ) {
95 ::srand( static_cast<unsigned int>(time(nullptr)));
96}
97
98int RandomGenerator::get( int lower, int upper ) {
99 int ret( 0 );
100 if ( GeneratorType::Standard == mType ) {
101 ret = ::rand() % upper + lower;
102 } else if (GeneratorType::MersenneTwister == mType) {
103 ret = mersenne_twister() % upper + lower;
104 }
105
106 return ret;
107}
108
109} // Namespace cppcore
RandomGenerator(GeneratorType type=GeneratorType::Standard) noexcept
The class constructor.
int get(int lower, int upper)
Gets a new random number.
GeneratorType
This enum describes the requested random generator.
@ MersenneTwister
The MersenneTwister algorithm.
@ Standard
The default c+++ generator.
unsigned int mersenne_twister()
static bool mersenne_twister_vector_update(unsigned int *const p)
static bool mersenne_twister_vector_init(unsigned int *seedPoints, size_t len)
static constexpr unsigned int M
static constexpr unsigned int N