Jovi Zhihao Zhou Jovi Zhihao Zhou - 24 days ago 12
C++ Question

How to use uniform_real_distribution to generate different numbers?

I used default_random_engine to generate a list of different numbers, but they happen to be the same. I wonder to generate different numbers? I know srand() does. But can I use this default engine?

default_random_engine engine{ static_cast<unsigned int>(time(0)) };
uniform_real_distribution<double>randomNumber{ 50,100 };
for(int i=0; i<10;i++){
a[i]= randomNumber(engine);
}

Jay Jay
Answer

That behavior is by design for random number generators. You need to set the 'seed' for the random generator when you start your program.

If you have the C++ extensions @WhozGraig recommends they're best. It's very sloppy but many people use the time() function to get a different number each time the program runs.

Comments