Standard library header <random>
std::linear_congruential_engine
template<
class UIntType,
UIntType a,
UIntType c,
UIntType m
> class linear_congruential_engine;
-
Template parameters
- UIntType: The result type generated by the generator. The effect is undefined if this is not one of unsigned short, unsigned int, unsigned long, or unsigned long long.
a: the multiplier term
c: the increment term
m: the modulus term
When m is not zero, if a >= m or c>= m is true, the program is ill-formed.
-
Generator properties
The size of the states of linear_congruential_engine is 1, each of them consists of a single integer.
The actual modulus m0 is defined as follows:
-
If m is not zero, m0 is m.
-
If m is zero, m0 is the value of std::numeric_limits<result_type>::max() plus 1
The transition algorithm of linear_congruential_engine is TA(xi) = (a·xi+c) mod m0.
The generation algorithm of linear_congruential_engine is GA(xi) = (a·xi+c) mod m0.
The pseudo-random number generated with the current state is also the successor state.
-
Predefined specializations
| Type |
Definition |
| minstd_rand0 (C++11) |
std::linear_congruential_engine<std::uint_fast32_t, 16807, 0, 2147483647> |
| minstd_rand (C++11) |
std::linear_congruential_engine<std::uint_fast32_t, 48271, 0, 2147483647> |
| Type |
Definition |
| result_type |
UintType |
| Function |
Definition |
| (constructor) |
constructs the engine |
| seed |
sets the current state of the engine |
| operator() |
advances the engine’s state and returns the generated value |
| discard |
advances the engine’s state by a specified amount |
| min (static) |
gets the smallest possible value in the output range |
| max (static) |
gets the largest possible value in the output range |
| Function |
Definition |
| operator== (C++11) / operator!=(C++11)(removed in C++20) |
compares the internal states of two pseudo-random number engines |
| operator<< / operator>> (C++11) |
performs stream input and output on pseudo-random number engine |
#include <iostream>
#include <random>
int main() {
std::random_device random_device;
std::minstd_rand engine(random_device());
for (int i = 0; i < 10; i++) {
std::cout << engine() << std::endl;
}
}
std::mersenne_twister_engine
template<
class UIntType, std::size_t w, std::size_t n, std::size_t m, std::size_t r,
UIntType a, std::size_t u, UIntType d, std::size_t s,
UIntType b, std::size_t t, UIntType c, std::size_t l, UIntType f
> class mersenne_twister_engine;
mersenne_twister_engine is a random number engine based on Mersenne Twister algorithm. It produces high quality, but not cryptographically secure, unsigned integer random numbers of type UIntType on the interval [0, 2w).
-
Template parameters
- UIntType: The result type generated by the generator. The effect is undefined if this is not one of unsigned short, unsigned int, unsigned long, or unsigned long long.
- w: the power of two that determines the range of values generated by the engine
- n: the degree of recurrence
- m: the middle word, an offset used in the recurrence relation defining the state
- r: the number of bits of the lower bit-mask, also known as the twist value
- a: the conditional xor-mask, i.e. the coefficients of the rational normal form twist matrix
- u, d, s, b, t, c, l: the 1st to 7th components of the bit-scrambling (tempering) matrix
- f: the initialization multiplier
-
Generator properties
The size of the states of mersenne_twister_engine is n, each of them consists of a sequence X of n values of type result_type. Xj stands for the j mod nth value (starting from 0) of X.
Given the following bitwise operation notations:
- bitand, built-in bitwise AND.
- xor, built-in bitwise XOR.
- lshift, built-in bitwise left-shift.
- rshift, built-in bitwise right-shift.
The transition algorithm of mersenne_twister_engine (TA(xi)) is defined as follows:
1. Concatenate the upper w - r bits of Xi-n with the lower r bits of Xi+1-n to obtain an unsigned integer value Y.
2. Let y be a·(Y bitand 1), and set Xi to Xi+m−n xor (Y rshift 1) xor y.
The generation algorithm of mersenne_twister_engine (GA(xi)) is defined as follows:
1. Let z1 be Xi xor ((Xi rshift u) bitand d).
2. Let z2 be Xi xor (((Xi lshift s) mod 2w) bitand b).
3. Let z3 be Xi xor (((Xi lshift t) mod 2w) bitand c).
4. Let z4 be z3 xor (z3 rshift l).
5. Deliver z4 as the result (i.e. GA(xi)=z4).
- Predefined specializations
| Type |
Definition |
| mt19937 |
std::mersenne_twister_engine<std::uint_fast32_t, 32, 624, 397, 31, 0x9908b0df, 11, 0xffffffff, 7, 0x9d2c5680, 15, 0xefc60000, 18, 1812433253> |
| mt_19937_64 |
std::mersenne_twister_engine<std::uint_fast64_t, 64, 312, 156, 31, 0xb5026f5aa96619e9, 29, 0x5555555555555555, 17, 0x71d67fffeda60000, 37, 0xfff7eee000000000, 43, 6364136223846793005> |
| Type |
Definition |
| result_type |
UIntType |
-
Data members
w: constexpr size_t word_size
n: constexpr size_t state_size
m: constexpr size_t shift_size
r: constexpr size_t mask_bits
a: constexpr UIntType xor_mask
u: constexpr size_t tempering_u
d: constexpr UIntType tempering_d
s: constexpr size_t tempering_s
b: constexpr UIntType tempering_b
t: constexpr size_t tempering_t
c: constexpr UIntType tempering_c
l: constexpr size_t tempering_l
f: constexpr UIntType initialization_multiplier
5489u: constexpr UIntType default_seed
-
Member functions
| Function |
Definition |
| (constructor) |
constructs the engine |
| seed |
sets the current state of the engine |
| operator() |
advances the engine’s state and returns the generated value |
| discard |
advances the engine’s state by a specified amount |
| min (static) |
gets the smallest possible value in the output range |
| max (static) |
gets the largest possible value in the output range |
| Function |
Definition |
| operator== (C++11) / operator!=(C++11)(removed in C++20) |
compares the internal states of two pseudo-random number engines |
| operator<< / operator>> (C++11) |
performs stream input and output on pseudo-random number engine |
#include <iostream>
#include <random>
int main() {
std::random_device random_device;
std::mt19937 engine(random_device());
for (int i = 0; i < 10; i++) {
std::cout << engine() << std::endl;
}
}
Reference
1.cppreference