Skip to main content
GameDev.net gamedev.net
🔒 Locked

Why Are My Random Numbers Not Random?

Started by X Abstract X Jun 8, 2010 at 8:07 PM 7 replies 1.9k views
Original Post
X Abstract X
X Abstract X
I was under the impression I wouldn't have to seed my generator in the following code but all of the random numbers generated are the same. What's wrong?

namespace Engine {namespace Math {static boost::mt19937 randomGenerator;static inline float randomFloat(float min, float max) {    boost::uniform_real<float> distribution(min, max);    boost::variate_generator<boost::mt19937, boost::uniform_real<float>> random(randomGenerator, distribution);    return random();}} //namespace Math} //namespace Engine
diablos_blade
diablos_blade
I had this "problem" too. The reason problem is in quotations is that it is clearly a design choice on the part of boost (it is possible you may want to generate more than one random number from an engine state), but it does seem slightly unintuitive.

Your "random" variable takes a copy of "randomGenerator" in it's constructor.
This copy is then used to generate the random output and is then destructed when "random" is destructed at the end of the function. So "randomGenerator" is never updated with the latest state.

What I did, which I'm not entirely sure is the right thing to do, was something like this:

static inline float randomFloat(float min, float max) {    boost::uniform_real<float> distribution(min, max);    boost::variate_generator<boost::mt19937, boost::uniform_real<float>> random(randomGenerator, distribution);    float out = random();    randomGenerator = random.engine( );    return out;}


But I was hacking at the time to get some University work done on time so I really can't recomend it as an option. It works, but seemed horrible as I was writing it.
X Abstract X
X Abstract X
diablos_blade: Thanks for explaining :D

sebas: Thank you, that works and makes sense.
erissian
erissian
Quote:
Original post by OneThreeThreeSeven
learn to write your own functions D:


Did you have something to contribute to that process?
We''re sorry, but you don''t have the clearance to read this post. Please exit your browser at this time. (Code 23)
Brainsaw
Brainsaw
I don't know about the boost random generator, but random number in general are not random. The old-style random generation (function rand()) has to be initialized with a "random" seed (normally a timestamp), otherwise it will produce the same random numbers. See http://www.cplusplus.com/reference/clibrary/cstdlib/rand/ for details.
XTAL256
XTAL256
I'm surprised no one has posted this yet [grin]

XKCD random number
[Window Detective] - Windows UI spy utility for programmers
X Abstract X
X Abstract X
Quote:
Original post by Brainsaw
I don't know about the boost random generator, but random number in general are not random. The old-style random generation (function rand()) has to be initialized with a "random" seed (normally a timestamp), otherwise it will produce the same random numbers. See http://www.cplusplus.com/reference/clibrary/cstdlib/rand/ for details.


Yes the boost method shown here will generate the same series of numbers everytime the program is run. That's not an issue for my case though, so I won't bother seeding.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.