-4

I have some problem when generating random number with boost library in CPP. When I try printout random number, the value return same value. Here is my code.

for(int i = 0; i < TOTAL_PARTICLES; i++) {
            boost::random::mt19937 engine(static_cast<unsigned int>(std::time(0)));                    
            randn = boost::bind(boost::random::uniform_real_distribution<>(-2.5, 2.5), engine);                            
            cout << "Random : " << randn() << endl;
        }
Eko Rudiawan
  • 187
  • 3
  • 9
  • 2
    You're always using the same seed - try putting the engine out the for loop – UKMonkey May 28 '19 at 15:10
  • different generator, same problem: https://stackoverflow.com/questions/9459035/why-does-rand-yield-the-same-sequence-of-numbers-on-every-run btw I found this by simply typing your title into google, hence the downvote ;) – 463035818_is_not_an_ai May 28 '19 at 15:12

1 Answers1

1

You're using the same engine seed every time, and therefore you'll receive the same set of random numbers each and every time. Therefore simply seed the engine outside of the for loop like so:

boost::random::mt19937 engine(static_cast<unsigned int>(std::time(0)));                    
for(int i = 0; i < TOTAL_PARTICLES; i++) 
{
    randn = boost::bind(boost::random::uniform_real_distribution<>(-2.5, 2.5), engine);                            
    cout << "Random : " << randn() << endl;
}
Dylan Gentile
  • 146
  • 1
  • 5