Old way
use rand()
, usually pair with a random initialization of the seed:
|
|
get random int in [0,x)
: rand()%x
get random real in [0, 1]
: rand()/double(RAND_MAX)
Modern way
Generators
random_device
random_device is a uniformly-distributed integer random number generator that produces non-deterministic random numbers.
A good implementation should has its randomness come from a non-deterministic source (e.g. a hardware device).
We can just use this to get our random numbers, but it might come with a light performance price. A PRNG is much better
pseudo RNG
So, we usually use random_device to get our first random number, then use it to initialize other PRNGs, then we can more quickly get many more random numbers.
The STL implements several PRNGs (see cppref: Predefined random number generators), not just the Mersenne Twister shown above, you can check
Distributions
|
|
Use cases of PRNG
TODO…