Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generating number (0,1) using mersenne twister c++

I'm working on implementing R code into C++ so that it runs faster, but I am having difficulties implementing mersenne twister. I only wish to generate values between (0,1). Here is what I have that pertains to this question.

#include <random>

std::mt19937 generator (123);

std::cout << "Random value: " << generator() << std:: endl;

I tried dividing by RAND_MAX, but that did not produce the values that I was looking for.

Thanks in advance.

like image 524
user3508622 Avatar asked Apr 07 '14 21:04

user3508622


3 Answers

In C++11 the concepts of "(pseudo) random generator" and "probability distribution" are separated, and for good reasons.

What you want can be achieved with the following lines:

  std::mt19937 generator (123);
  std::uniform_real_distribution<double> dis(0.0, 1.0);

  double randomRealBetweenZeroAndOne = dis(generator);

If you want to understand why this separation is necessary, and why using a standard division /range manipulation on the output of the generator is a bad idea, watch this video.

like image 92
sbabbi Avatar answered Oct 02 '22 23:10

sbabbi


You may want to consider code like this:

// 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);

For more details on generating pseudo-random numbers in C++ (including reasons why rand() is not good), see this video by Stephan T. Lavavej (from Going Native 2013):

rand() Considered Harmful

like image 22
Mr.C64 Avatar answered Oct 02 '22 21:10

Mr.C64


std::mt19937 does not generate between 0 and RAND_MAX like rand(), but between 0 and 2^32-1

And by the way, the class provides min() and max() values!

You need to convert the value to a double, substract min() and divide by max()-min()

uint32_t val;
val << generator;
double doubleval = ((double)val - generator::min())/(generator::max()-generator::min());

or (less generic)

uint32_t val;
val << generator;
double doubleval = (double)val * (1.0 / std::numeric_limits<std::uint32_t>::max());
like image 36
galinette Avatar answered Oct 02 '22 23:10

galinette