This page looks best with JavaScript enabled

Use Random in C++

 ·   ·  โ˜• 1 min read · ๐Ÿ‘€... views

Old way

use rand(), usually pair with a random initialization of the seed:

1
2
srand(int(time(0))); // initialize the seed
rand(); // get a random int, [0, RAND_MAX]

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

1
2
3
4
5
// ...
#include <random>

std::random_device rd; // get a seed
std::mt19937 g(rd());

Use cases of PRNG

TODO…

Refs

Share on