15#ifndef ARCANE_CORE_RANDOM_MERSENNETWISTER_H
16#define ARCANE_CORE_RANDOM_MERSENNETWISTER_H
20#include "arcane/utils/FatalErrorException.h"
22#include "arcane/core/random/RandomGlobal.h"
27namespace Arcane::random
51 typedef UIntType result_type;
52 static const Integer word_size = w;
53 static const Integer state_size = n;
54 static const Integer shift_size = m;
55 static const Integer mask_bits = r;
56 static const UIntType parameter_a = a;
57 static const Integer output_u = u;
58 static const Integer output_s = s;
59 static const UIntType output_b = b;
60 static const Integer output_t = t;
61 static const UIntType output_c = c;
62 static const Integer output_l = l;
64 static const bool has_fixed_range =
false;
112 template <
class Generator>
138 const UIntType mask = ~0u;
140 for (i = 1; i < n; i++) {
142 x[i] = (1812433253UL * (x[i - 1] ^ (x[i - 1] >> (w - 2))) + i) & mask;
157 for (
Integer i = 0; i < n; i++)
171 template <
class Generator>
177 for (
Integer j = 0; j < n; j++)
205 result_type
min()
const {
return 0; }
219 for (
Integer i = 0; i < w; ++i)
251 for (
Integer j = 0; j < state_size; ++j)
267 return !(*
this == rhs);
285 return x[(i + n + index) % (2 * n)];
313void MersenneTwister<UIntType, w, n, m, r, a, u, s, b, t, c, l, val>::twist(
int block)
315 const UIntType upper_mask = (~0u) << r;
316 const UIntType lower_mask = ~upper_mask;
319 for (
Integer j = n; j < 2 * n; j++) {
320 UIntType y = (x[j - n] & upper_mask) | (x[j - (n - 1)] & lower_mask);
321 x[j] = x[j - (n - m)] ^ (y >> 1) ^ (y & 1 ? a : 0);
324 else if (block == 1) {
327 for (
Integer j = 0; j < n - m; j++) {
328 UIntType y = (x[j + n] & upper_mask) | (x[j + n + 1] & lower_mask);
329 x[j] = x[j + n + m] ^ (y >> 1) ^ (y & 1 ? a : 0);
333 for (
Integer j = n - m; j < n - 1; j++) {
334 UIntType y = (x[j + n] & upper_mask) | (x[j + n + 1] & lower_mask);
335 x[j] = x[j - (n - m)] ^ (y >> 1) ^ (y & 1 ? a : 0);
338 UIntType y = (x[2 * n - 1] & upper_mask) | (x[0] & lower_mask);
339 x[n - 1] = x[m - 1] ^ (y >> 1) ^ (y & 1 ? a : 0);
355inline typename MersenneTwister<UIntType, w, n, m, r, a, u, s, b, t, c, l, val>::result_type
356MersenneTwister<UIntType, w, n, m, r, a, u, s, b, t, c, l, val>::operator()()
377 7, 0x31b6ab00, 15, 0xffe50000, 17, 0xa37d3c92>
385 7, 0x9d2c5680, 15, 0xefc60000, 18, 3346425566U>
MersenneTwister class pattern.
static bool validation(result_type v)
Validation function (I don't really know what it is for!).
MersenneTwister()
Constructor with seed initialization from the seed() method.
MersenneTwister(UIntType value)
Constructor with initialization of the seed array from the value value. The call to the seed(value) m...
bool operator!=(const MersenneTwister &rhs) const
Overriding the != operator.
void seed(UIntType *state)
Initialization of the seed array from the array state. state must be an array of n elements.
void seed(UIntType value)
Initialization of the seed array from the value value. The seed array of this generator consists of n...
MersenneTwister(Generator &gen)
Constructor with initialization of the seed array from the generator gen. gen must contain the () ope...
result_type max() const
max() returns the maximum possible value of a sequence.
void seed(Generator &gen)
Initialization of the seed array from the generator gen. gen is a class that must contain the () oper...
UIntType getState(Integer j)
Method that returns the generator state for index j. The complete state of the generator is given by ...
bool operator==(const MersenneTwister &rhs) const
Overriding the == operator.
void twist(Integer block)
Implementation of the "twist" operation associated with the Mersenne Twister. The generator state is ...
UIntType compute(UIntType index) const
Private method that returns the generator state for index index.
MersenneTwister(It &first, It last)
Constructor with initialization of the seed array from the seed(first,last) method.
void seed()
Initialization of the seed array. The call to the seed(5489) method is performed.
result_type operator()()
Overriding the () operator which returns the pseudo random value of the generator....
result_type min() const
min() returns the minimum possible value of a sequence.
std::uint32_t UInt32
Unsigned integer type of 32 bits.
Int32 Integer
Type representing an integer.