Вопрос

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.

Это было полезно?

Решение

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.

Другие советы

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

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());
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top