C++ random Library
C++ random Library
The <random> library creates random numbers. It has two parts that work together:
- An engine creates random numbers.
- A distribution turns them into the kind of numbers you want, like a whole number between 1 and 6.
For most programs, use a random_device to start an mt19937 engine, and a distribution to get the numbers you need.
Engines
| Class | Description |
|---|---|
| random_device | Creates a random number from the computer, often used as the starting value (seed) for an engine |
| default_random_engine | A random number engine that is easy to use |
| mt19937 | A fast, high-quality random number engine (Mersenne Twister) |
Distributions
| Class | Description |
|---|---|
| uniform_int_distribution | Gives random whole numbers in a range, where every number is equally likely |
| uniform_real_distribution | Gives random decimal numbers in a range |
| normal_distribution | Gives random decimal numbers that are most often close to an average value |
| bernoulli_distribution | Gives a random true or false, like a coin flip |
Related Pages
C++ How To Generate a Random Number