9 #ifndef CRYPTOPP_MERSENNE_TWISTER_H 10 #define CRYPTOPP_MERSENNE_TWISTER_H 18 template <
unsigned int K,
unsigned int M,
unsigned int N,
unsigned int F,
unsigned long S>
41 for (
unsigned int i = 1; i < N+1; i++)
42 m_state[i] =
word32(F * (m_state[i-1] ^ (m_state[i-1] >> 30)) + i);
56 for (
size_t i=0; i < size/4; i++, output += 4)
58 #if defined(CRYPTOPP_ALLOW_UNALIGNED_DATA_ACCESS) && defined(IS_LITTLE_ENDIAN) 60 #elif defined(CRYPTOPP_ALLOW_UNALIGNED_DATA_ACCESS) 61 *((
word32*)output) = NextMersenneWord();
63 temp = NextMersenneWord();
75 *((
volatile word32*)&temp) = 0;
80 temp = NextMersenneWord();
91 *((
volatile word32*)&temp) = 0;
101 if (range == 0xffffffffL)
102 return NextMersenneWord();
108 value =
Crop(NextMersenneWord(), maxBits);
109 }
while (value > range);
134 if (m_idx >= N) { Twist(); }
136 word32 temp = m_state[m_idx++];
138 temp ^= (temp >> 11);
139 temp ^= (temp << 7) & 0x9D2C5680;
140 temp ^= (temp << 15) & 0xEFC60000;
142 return temp ^ (temp >> 18);
148 static const unsigned long magic[2]={0x0
UL, K};
152 for (kk=0;kk<N-M;kk++)
154 temp = (m_state[kk] & 0x80000000)|(m_state[kk+1] & 0x7FFFFFFF);
155 m_state[kk] = m_state[kk+M] ^ (temp >> 1) ^ magic[temp & 0x1UL];
160 temp = (m_state[kk] & 0x80000000)|(m_state[kk+1] & 0x7FFFFFFF);
161 m_state[kk] = m_state[kk+(M-N)] ^ (temp >> 1) ^ magic[temp & 0x1
UL];
164 temp = (m_state[N-1] & 0x80000000)|(m_state[0] & 0x7FFFFFFF);
165 m_state[N-1] = m_state[M-1] ^ (temp >> 1) ^ magic[temp & 0x1UL];
171 *((
volatile word32*)&temp) = 0;
190 #if CRYPTOPP_DOXYGEN_PROCESSING 203 #if CRYPTOPP_DOXYGEN_PROCESSING 211 #endif // CRYPTOPP_MERSENNE_TWISTER_H
Utility functions for the Crypto++ library.
void Twist()
Performs the twist operaton on the state array.
MersenneTwister< 0x9908B0DF, 397, 624, 0x10DCD, 4537 > MT19937
FixedSizeSecBlock< word32, N+1 > m_state
32-bit word state array of size N
#define NAMESPACE_BEGIN(x)
Mersenne Twister class for Monte-Carlo simulations.
unsigned int m_idx
the current index into the state array
Abstract base classes that provide a uniform interface to this library.
Interface for random number generators.
Classes and functions for secure memory allocations.
T Crop(T value, size_t bits)
Truncates the value to the specified number of bits.
void DiscardBytes(size_t n)
Generate and discard n bytes.
unsigned int m_seed
the value used to seed the generator
Original MT19937 generator provided in the ACM paper.
void GenerateBlock(byte *output, size_t size)
Generate random array of bytes.
#define CRYPTOPP_ASSERT(exp)
word32 NextMersenneWord()
Returns the next 32-bit word from the state array.
MersenneTwister< 0x9908B0DF, 397, 624, 0x6C078965, 5489 > MT19937ar
Updated MT19937 generator adapted to provide an array for initialization.
uint8_t const size_t const size
T1 RoundUpToMultipleOf(const T1 &n, const T2 &m)
Rounds a value up to a multiple of a second value.
#define CRYPTOPP_GET_BYTE_AS_BYTE(x, y)
word32 GenerateWord32(word32 min=0, word32 max=0xffffffffL)
Generate a random 32-bit word in the range min to max, inclusive.
byte ByteReverse(byte value)
Reverses bytes in a 8-bit value.
unsigned int BitPrecision(const T &value)
Returns the number of bits required for a value.
MersenneTwister(unsigned long seed=S)
Construct a Mersenne Twister.