Using boost::random and getting same sequence of numbers

前端 未结 6 715
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-19 19:04

I have the following code:

Class B {

void generator()
{
    // creating random number generator
    boost::mt19937 randgen(static_cast(s         


        
6条回答
  •  无人及你
    2020-12-19 19:47

    A late answer: two random-number generator functions for comparing boost with standard method.

    boost

    #include 
    
    //the code that uses boost is massively non-intuitive, complex and obfuscated
    
    bool _boost_seeded_=false;
    
    /*--------------------*/int
    boostrand(int High, int Low)
    {
        static boost::mt19937 random;
        if (!_boost_seeded_)
        {
            random = boost::mt19937(time(0));
            _boost_seeded_=true;
        }
        boost::uniform_int<> range(Low,High);
        boost::variate_generator > 
            getrandom(random, range);
    
        return getrandom();
    }
    

    standard

    #include 
    #include 
    
    //standard code is straight-forward and quite understandable
    
    bool _stdrand_seeded_=false;
    
    /*--------------------*/int
    stdrand(int High, int Low)
    {
        if (!_stdrand_seeded_)
        {
            srand(time(0));
            _stdrand_seeded_=true;
        }
        return ((rand() % (High - Low + 1)) + Low);
    }
    

    The results from both functions are comparably of the same "randomness". I would apply the KISS-principle.

提交回复
热议问题