1184 Numerics Chapter 40 Standard Random Number Engines

advertisement
1184
Numerics
Chapter 40
Standard Random Number Engines (§iso.26.5.3)
default_random_engine
linear_congruential_engine<UI,a,c,m>
mersenne_twister_engine<UI,w,n,m,r,a,u,d,s,t,c,l,f>
subtract_with_carry_engine<UI,w,s,r>
An alias for an engine with
wide applicability and low cost
x i1 (ax i c) mod m
§iso.26.5.3.2
x i1 (ax i ) mod b
where b m r m s 1
and a b (b 1)/m
The UI parameter for a standard random number engine must be an unsigned integer type. For linear_congruential_engine<UI,a,c,m>,
if the modulus m is 0, the value numeric_limits<result_type>::max()+1 is used. For example, this writes out the index of the first repetition of a
number:
map<int,int> m;
linear_congruential_engine<unsigned int,17,5,0> linc_eng;
for (int i=0; i<1000000; ++i)
if (1<++m[linc_eng()]) cout << i << '\n';
I was lucky; the parameters were not too bad and I got no duplicate values. Try <unsigned
int,16,5,0> instead and see the difference. Use the default_random_engine unless you have a real
need and know what you are doing.
A random number engine adaptor takes a random number engine as an argument and produces
a new random number engine with different randomness properties.
Standard Random Number Engine Adaptors (§iso.26.5.4)
discard_block_engine<E,p,r>
independent_bits_engine<E,w,UI>
shuffle_order_engine<E,k>
is the engine; §iso.26.5.4.2
Generate w bits in type UI; §iso.26.5.4.3
§iso.26.5.4.4
E
For example:
independent_bits_engine<default_random_engine,4,unsigned int> ibe;
for (int i=0; i<100; ++i)
cout << '0'+ibe() << ' ';
This will produce 100 numbers in the range [48:63] ([’0’:’0’+24 -1)).
A few aliases are defined for useful engines:
using minstd_rand0 = linear_congruential_engine<uint_fast32_t, 16807, 0, 2147483647>;
using minstd_rand = linear_congruential_engine<uint_fast32_t, 48271, 0, 2147483647>;
using mt19937 = mersenne_twister_engine<uint_fast32_t, 32,624,397,
31,0x9908b0df,
11,0xffffffff,
7,0x9d2c5680,
15,0xefc60000,
18,1812433253>
Section 40.7.1
Engines
1185
using mt19937_64 = mersenne_twister_engine<uint_fast64_t, 64,312,156,
31,0xb5026f5aa96619e9,
29, 0x5555555555555555,
17, 0x71d67fffeda60000,
37, 0xfff7eee000000000,
43, 6364136223846793005>;
using ranlux24_base = subtract_with_carry_engine<uint_fast32_t, 24, 10, 24>;
using ranlux48_base = subtract_with_carry_engine<uint_fast64_t, 48, 5, 12>;
using ranlux24 = discard_block_engine<ranlux24_base, 223, 23>;
using ranlux48 = discard_block_engine<ranlux48_base, 389, 11>;
using knuth_b = shuffle_order_engine<minstd_rand0,256>;
40.7.2 Random Device
If an implementation is able to offer a truly random number generator, that source of random numbers is presented as a uniform random number generator called random_device:
random_device
random_device rd {s};
d=rd.entropy()
(§iso.26.5.6)
The string s identifies a source of random numbers;
implementation-defined; explicit
d is a double; d==0.0 for a pseudo-random number generator
Think of s as the name of a random number source, such as a Geiger counter, a Web service, or a
file/device containing the record of a truly random source. The entropy() is defined as
S(P 0 , . . . , P n1 ) 
in1

P i log P i
i0
for a device with n states whose respective probabilities are P 0 , . . . , P n1 . The entropy is an estimate of the randomness, the degree of unpredictability, of the generated numbers. In contrast to
thermodynamics, high entropy is good for random numbers because that means that it is hard to
guess subsequent numbers. The formula reflects the result of repeatedly throwing a perfect n-sided
dice.
The random_device is intended to be useful for cryptograpic applications, but it would be
against all rules of that kind of application to trust an implementation of random_device without
first studying it closely.
40.7.3 Distributions
A random number distribution is a function object that, when called with a random number generator argument, produces a sequence of values of its result_type:
Random Number Distribution D (§iso.26.5.1.6) (continues)
D::result_type
D::param_type
D d {};
D d {p};
The type of an element of D
The type of the set of arguments needed to construct a D
Default constructor
Construct from param_type p
Download