Since C++11, the C++ standard library provides a random-number library. The library provides many well-known distributions, and in addition, it provides multiple engines, which are the sources of randomness. To use the lib, include <random> header file.
1) The engines
serve as a stateful source of randomness. They are function objects that are able to generate random unsigned values uniformly distributed according to a predefined minimum and maximum.Predefined engines:
serve as a way to specify how to use these random values to create random numbers that are distributed over a range of values according to user-supplied parameters.
Predefined distributions:
Distribution operations:
#include <map>
#include <string>
#include <iostream>
#include <iomanip>
template <typename Distr, typename Eng>
void distr (Distr d, Eng e, const std::string& name)
{
// print min, max and four example values
std::cout << name << ":" << std::endl;
std::cout << "- min(): " << d.min() << std::endl;
std::cout << "- max(): " << d.max() << std::endl;
std::cout << "- values: " << d(e) << ’ ’ << d(e) << ’ ’ << d(e) << ’ ’ << d(e) << std::endl;
// count the generated values (converted to integral values)
std::map<long long,int> valuecounter;
for (int i=0; i<200000; ++i) {
valuecounter[d(e)]++;
}
// and print the resulting distribution
std::cout << "====" << std::endl;
for (auto elem : valuecounter) {
std::cout << std::setw(3) << elem.first << ": " << elem.second << std::endl;
}
std::cout << "====" << std::endl;
std::cout << std::endl;
}
int main()
{
std::knuth_b e;
std::uniform_real_distribution<> ud(0, 10);
distr(ud,e,"uniform_real_distribution");
std::normal_distribution<> nd;
distr(nd,e,"normal_distribution");
std::exponential_distribution<> ed;
distr(ed,e,"exponential_distribution");
std::gamma_distribution<> gd;
distr(gd,e,"gamma_distribution");
}
1) The engines
serve as a stateful source of randomness. They are function objects that are able to generate random unsigned values uniformly distributed according to a predefined minimum and maximum.Predefined engines:
Engine operations:
serve as a way to specify how to use these random values to create random numbers that are distributed over a range of values according to user-supplied parameters.
Predefined distributions:

Distribution operations:

3) Use "random_device" as seed, mt19937 (mt19937_64) as generator, uniform_int_distribution<> as distributor. (fast, perfect uniform distribution)
std::random_device rd;
std::mt19937 mt(rd());
std::uniform_int_distribution<int> dist(0, 99);
for (int i = 0; i < 16; ++i) {
std::cout << dist(mt) << " ";
}
std::cout << std::endl;
4) "random" considered harmful (non-uniform distributed). "random_shuffle" considered harmful (may call "random"). "shuffle" is awesome.
5) Example
#include <random>#include <map>
#include <string>
#include <iostream>
#include <iomanip>
template <typename Distr, typename Eng>
void distr (Distr d, Eng e, const std::string& name)
{
// print min, max and four example values
std::cout << name << ":" << std::endl;
std::cout << "- min(): " << d.min() << std::endl;
std::cout << "- max(): " << d.max() << std::endl;
std::cout << "- values: " << d(e) << ’ ’ << d(e) << ’ ’ << d(e) << ’ ’ << d(e) << std::endl;
// count the generated values (converted to integral values)
std::map<long long,int> valuecounter;
for (int i=0; i<200000; ++i) {
valuecounter[d(e)]++;
}
// and print the resulting distribution
std::cout << "====" << std::endl;
for (auto elem : valuecounter) {
std::cout << std::setw(3) << elem.first << ": " << elem.second << std::endl;
}
std::cout << "====" << std::endl;
std::cout << std::endl;
}
int main()
{
std::knuth_b e;
std::uniform_real_distribution<> ud(0, 10);
distr(ud,e,"uniform_real_distribution");
std::normal_distribution<> nd;
distr(nd,e,"normal_distribution");
std::exponential_distribution<> ed;
distr(ed,e,"exponential_distribution");
std::gamma_distribution<> gd;
distr(gd,e,"gamma_distribution");
}