Answers for "how to use mersenne_twister_engine in c++ to generate random numbers"

C++
0

how to use mersenne_twister_engine in c++ to generate random numbers

// For pseudo-random number generators and distributions
#include <random> 

...
    
// Use random_device to generate a seed for Mersenne twister engine.
std::random_device rd{};    

// Use Mersenne twister engine to generate pseudo-random numbers.
std::mt19937 engine{rd()};

// "Filter" MT engine's output to generate pseudo-random double values,
// **uniformly distributed** on the closed interval [0, 1].
// (Note that the range is [inclusive, inclusive].)
std::uniform_real_distribution<double> dist{0.0, 1.0};

// Generate pseudo-random number.
double x = dist(engine);
Posted by: Guest on May-27-2021

Code answers related to "how to use mersenne_twister_engine in c++ to generate random numbers"

Browse Popular Code Answers by Language