normal distribution

Hi all,

I have the following code for getting a double from a normal distribution.
I call the function during several times in a program but it gives the same result everytime. Could anyone help me please? How can I make it, give different results each time I call it?

float getnormal()
mt19937 eng;
normal_distribution<float> normal(0.0, 0.4);
float n = normal(eng);
return n;
Have you read this from the reference section?

Edit: Presumably you know that the function takes no args, so will return the same each time.

HTH Cheers

Oh and code tags always would be good. The <> button on the right.
Last edited on
You need to seed the random number generator.

mt19937 eng(std::time(0));
@Peter87: It doesn't work.

@TheIdeasMan: Yes I read this

I use Visual Studio 2010 and it could not open the library chrono.

I could not understand your edit????
Your function creates a new random number generator every time, seeded with zero (or with time(), after you tried Peter87's suggestion), but the next time you call this function, you're recreating the random number generator, and you're re-seeding it with zero (or with time(), which likely hasn't changed since a full second didn't pass)

Only create the RNG once, and pass it to your function by reference.
@Cubbi: This also doesn't work. It gives different values within one run but I get the same sequence of numbers everytime I run the program.
std::tr1::mt19937 eng;
eng.seed((unsigned int)time(NULL));  //Do this once 

float getnormal(std::tr1::mt19937& eng);

float getnormal(eng);

This works for me, different randoms each time program runs, or in loops, doesn't matter.
@Clanmjc: That worked. Thank you very much.
^The above was just code for the words Cubbi said, credit @Cubbi. Glad it's working.
@Cubbi: And also thank you very much. With you guys' help I solved the problem.
Topic archived. No new replies allowed.