Using boost :: random and getting the same sequence of numbers

I have the following code:

Class B {

void generator()
{
    // creating random number generator
    boost::mt19937 randgen(static_cast<unsigned int>(std::time(0)));
    boost::normal_distribution<float> noise(0,1);
    boost::variate_generator<boost::mt19937, 
        boost::normal_distribution<float> > nD(randgen, noise);


    for (int i = 0; i < 100; i++)
    {
        value = nD();
        // graph each value
    }
}
};

Class A {

void someFunction()
{
    for(int i = 1; i <=3; i++)
    {
        std::shared_ptr<B> b;
        b.reset(new B());
        b->generator();
    }
}
};

I want to execute the above code several times in a row to create multiple graphs. I also looked at this stackoverflow question , which is similar, but the caution states that when time (0) is used and the member function is called quickly, you will probably still get the same sequence of numbers.

How can I solve this problem?

EDIT: I tried to make randgen static in class B, also tried to make it a global variable in class A, but every time the 3 plots are the same. I also tried GetSystemTime millisecond sowing. Something is missing for me.

+3
6

- , .

, .

, . ( ), , , .

, Windows GetSystemTime(), SYSTEMTIME, - , Linux getTimeOfDay .

:

#include <windows.h>
SYSTEMTIME st;
GetSystemTime (&st);
// Use st.wSecond * 100 + st.wMillisecs to seed (0 thru 59999).

Linux:

#include <sys/time.h>
struct timeval tv;
gettimeofday (&tv, NULL);
// Use tv.tv_sec * 100 + (tv.tv_usec / 1000) to seed (0 thru 59999).
+6

Boost.Random - , . .

, , , , , :

std::ofstream generator_state_file("rng.saved");
generator_state_file << randgen;

, , :

std::ifstream generator_state_file("rng.saved");
generator_state_file >> randgen;

, .. ..

std::string std::stringstream, , .

+5

, :

static boost::mt19937 randgen(static_cast<unsigned int>(std::time(0)));
+4

unix /dev/random /dev/urandom . (0) + pid + ( ).

windows, QueryPerformanceCounter, .

:

mt19937 prng , .

:

" , ", . (, genGraph (int graphIndex) (add, xor ..) (0). boost::mt19937 randgen(static_cast<unsigned int>(std::time(0) + graphIndex));

+1

: boost .


#include <boost/random.hpp>

//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<boost::mt19937&, boost::uniform_int<> > 
        getrandom(random, range);

    return getrandom();
}


#include <cstdlib>
#include <time.h>

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

"". KISS.

+1

, ( (0)), .

time (0) has a resolution of 1 second. Using it several times, because the seed will create the same generator for a short period of time.

0
source

Source: https://habr.com/ru/post/1731143/


All Articles