wiki:hpp-random
<random>
<random> provides random number engines (std::mt19937, std::random_device) and distributions (std::uniform_int_distribution, std::normal_distribution). Engine and distribution are separate to allow reusing an engine with different distributions.
Always seed with a good entropy source: std::random_device() for unpredictable values, or a fixed seed for reproducibility.
Example
This example generates a random die roll and a normally-distributed random value using different distributions with the same engine.
// compile: g++ -std=c++11 -o randomexample randomexample.cpp // run: ./randomexample // description: random integers and normal distribution #include <random> #include <iostream> int main() { std::mt19937 gen(std::random_device{}()); std::uniform_int_distribution<int> dist(1, 6); std::cout << "die roll: " << dist(gen) << "\n"; std::normal_distribution<double> normal(0.0, 1.0); std::cout << "normal: " << normal(gen) << "\n"; return 0; }
wiki/hpp-random.md · Last modified: by 127.0.0.1
