//-----------------------------------------------------------------------------------
//Generate a positive random int between 0 and Max (inclusive)
//-----------------------------------------------------------------------------------
int CRandom::Random(int Max)
{
mRandCount++;
if (mRandCount>MAX_RANDS) SeedRand();
if(Max < 0) return 0;
return (Max + 1) * rand() / (RAND_MAX + 1);
}//Random(int)
//-----------------------------------------------------------------------------------
//Generate a random int between Min and Max (inclusive)
//If Min is not less than Max, this function will return 0
//-----------------------------------------------------------------------------------
int CRandom::Range(int Min, int Max)
{
mRandCount++;
if (mRandCount>MAX_RANDS) SeedRand();
if (Max < Min) return 0;
return ((Max - Min + 1) * rand() / (RAND_MAX + 1)) + Min;
}//Range(int, int)
Random Numbers--Is this right?
I''ve put together a class to generate random numbers, and it seems to work ok. But I have observed a trend where my numbers tend to clump toward the middle of a distribution, instead of being evenly distributed. Code is attached below.
Is it just happenstance that I see this trend, or is there really something non-random about my code? Thanks in advance!
Why do you reseed the generator? Most people just do it once at initialization and forget about it.
No, this is normal. In fact, it''s called a normal distribution - it''s a classic bell curve that emerges out of randomness everywhere in nature. Pick up a book on probability and get ready for some math if you really want to understand what''s happening.
So you're saying it's inevitable that I get a bell curve distribution? I don't want a bell curve distribution in my random numbers. Is there a way to avoid it?
EDIT: Mike, I re-seed the generator every 10000 random numbers, because the generator is based on the system time (which is not truly random). As more and more random numbers are generated, the sequence becomes more and more predictable; thus, to keep it as random as possible, I re-seed it periodically.
[edited by - Teric on May 6, 2003 2:31:53 PM]
EDIT: Mike, I re-seed the generator every 10000 random numbers, because the generator is based on the system time (which is not truly random). As more and more random numbers are generated, the sequence becomes more and more predictable; thus, to keep it as random as possible, I re-seed it periodically.
[edited by - Teric on May 6, 2003 2:31:53 PM]
I am not sure about this so i looked on mathworld...
http://mathworld.wolfram.com/RandomNumber.html
It does talk about some different distributions.
[edited by - gibber on May 6, 2003 4:14:16 PM]
http://mathworld.wolfram.com/RandomNumber.html
It does talk about some different distributions.
[edited by - gibber on May 6, 2003 4:14:16 PM]
A bell curve is not normal for a random number generator. I would try it without periodic reseeding and see if that makes a difference. If that doesn''t help, then it is probably coming from the rand() function and you may want to look at other random number generators.
Equidistribution is what you''re looking for - and for that I sugest using the Mersenne Twister. I''ve written an article over at CodeProject, you can find it here. I compare the speed of MT vs rand() with respect to speed and equidistribution...
Hope that helps, Game On!
Dave "Dak Lozar" Loeser
Hope that helps, Game On!
Dave "Dak Lozar" Loeser
Wrudyn is right, every number has the same probability to be ''picked'' by the rand() function. The number is not a true random number as it is a mathematical construction. I would try to use the rand() function without reseeding it.
----------------------
YNWA
----------------------
YNWA
unsigned rand_u32(cosnt unsigned& max) { if(max <= 0 || max > RAND_MAX) return 0; const unsigned bucketsize = RAND_MAX / max; unsigned a; do { a = rand() / bucketsize; } while(a >= max ); return a;}
....gives a ittle bit better distribution.
EDIT: source tags
[edited by - null_vector on May 7, 2003 2:07:49 PM]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement