Issues with c++ 11 mersenne_twister_engine class

故事扮演 提交于 2019-12-20 04:34:39

问题


I have been trying to use the c++ 11 mersenne_twister_engine class( http://www.cplusplus.com/reference/random/mersenne_twister_engine/) to generate numbers in the interval [0,1] however I am continually getting 0 on every number.

Here is my code snippet

unsigned seed1 = std::chrono::system_clock::now().time_since_epoch().count();   

std::mt19937 generator(seed1); 
for (int i=0; i < WIDTH * HEIGHT; i++) //GENERATES THE MATRIX OF CONNECTIVITY
{

    double random = generator()/generator.max();
    if ( random  > VERT_CONNECTIVITY )
    vert_con[i] = 0;
    else vert_con[i] = 1;


}

generator() and generator.max() seem to be working... It is just random which gives me 0!

Thanks!


回答1:


In C++, / on integer numbers results in an integer number. For example 11 / 2 results in 5, not 5.5. In the same way a / b is always zero if a < b.

Your problem is here:

generator()/generator.max()

generator() and generator.max() both return integers and of course, generator.max() >= generator(), so the result is zero (unless you are very lucky to get the max number in which case the result would be one).

To fix this, you can simply cast it:

(double)generator()/generator.max()



回答2:


Using one of the distributions provided in the random header probably makes more sense. As Cubbi points out std::bernoulli_distribution looks like it fits your problem well. It will generate true or false according to the distribution parameter you pass in:

#include <iostream>
#include <random>
#include <vector>
#include <algorithm>

const int WIDTH = 5 ;
const int HEIGHT = 5 ;
const double VERT_CONNECTIVITY = 0.25 ;

int main()
{
    std::random_device rd;
    std::mt19937 gen(rd());

    // give "true" 1/4 of the time
    // give "false" 3/4 of the time
    std::bernoulli_distribution d(VERT_CONNECTIVITY);

    std::vector<int> vert_con( WIDTH * HEIGHT ) ;

    std::generate( std::begin(vert_con), std::end( vert_con ), [&] () { return d(gen) ; } ) ;

    for (int i=0; i < WIDTH * HEIGHT; i++) 
    {
        std::cout << vert_con[i] << " " ;
    }
    std::cout << std::endl ;

    return 0 ;
}



回答3:


Perhaps you are looking for generate_canonical<double, bits>(gen)? This will generate a decimal value uniformly distributed in the interval [0,1) with bits bits of randomness (53 being the standard maximum available for a double).




回答4:


The mersenne_twister_engine returns results of type UIntType. If you want the result to be a double, you need to cast it to a double.

double random = (double)generator()/generator.max();



来源:https://stackoverflow.com/questions/20527043/issues-with-c-11-mersenne-twister-engine-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!