The header in C++ provides (pseudo-)random number generation (PRNG):
- UniformRandomBitGenerators produce random bits (entropy)
- RandomNumberDistributions use entropy to generate random numbers
These types are used via their overloaded call operators.
#include <random>
#include <iostream>
int main() {
std::random_device dev; // for seeding
std::default_random_engine gen{dev()};
std::uniform_int_distribution<int> dis{1, 6};
for (int i = 0; i < 10; ++i)
std::cout << dis(gen) << ' ';
}
1 1 6 5 2 2 5 5 6 2
?inline
- std::random_device: truly random
- std::default_random_engine
- std::mt19937: popular default choice
?inline
<:cppreference:875716540929015908>
Pseudo-random number generation
<:stackoverflow:874353689031233606>
Generate random numbers using C++11 random library
<:stackoverflow:874353689031233606>
Why is the use of rand() considered bad?