#### Archived

This topic is now archived and is closed to further replies.

# Random Numbers--Is this right?

This topic is 5555 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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!

//-----------------------------------------------------------------------------------

//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)



##### Share on other sites
Why do you reseed the generator? Most people just do it once at initialization and forget about it.

##### Share on other sites
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.

##### Share on other sites
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]

##### Share on other sites

http://mathworld.wolfram.com/RandomNumber.html

It does talk about some different distributions.

[edited by - gibber on May 6, 2003 4:14:16 PM]

##### Share on other sites
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.

##### Share on other sites
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

##### Share on other sites
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

##### Share on other sites

  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]

1. 1
2. 2
Rutin
22
3. 3
4. 4
JoeJ
16
5. 5

• 14
• 29
• 13
• 11
• 11
• ### Forum Statistics

• Total Topics
631775
• Total Posts
3002284
×