# RandNumber problem

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

## Recommended Posts

So the following if is called every 0.04 seconds,

if( 1 == RangedRandDemo(1,20) ){

}

How is it possible that the above statement be true 20 times in a row,and sometimes,it never is?

My random function is:
int RangedRandDemo( int range_min, int range_max )
{
// Generate random numbers in the half-closed interval
// [range_min, range_max). In other words,
// range_min <= random number < range_max
srand( (unsigned)time( NULL ) );
int i;

int u = (double)rand() / (RAND_MAX + 1) * (range_max - range_min) + range_min;

return u;
}

Can you give me a better one?

##### Share on other sites
it worked,thanks a lot!

##### Share on other sites
I have run into issues with the random number generator that did require calling srand again, but that is rare. Since it is a pseudo-random number generator, patterns can emerge that may be undesirable. If that happens, calling srand will resolve the issue. Chances are you'll never run into such an issue. That being said, alvaro is correct; srand should not be called every time rand is called.

I should note that the RNG I was using at the time was limited to 16-bit signed integers.

##### Share on other sites

I have run into issues with the random number generator that did require calling srand again, but that is rare. Since it is a pseudo-random number generator, patterns can emerge that may be undesirable. If that happens, calling srand will resolve the issue. Chances are you'll never run into such an issue. That being said, alvaro is correct; srand should not be called every time rand is called.

I should note that the RNG I was using at the time was limited to 16-bit signed integers.

Unless you intentionally wish to repeat a previous random sequence, calling srand again is undesireable.
Re-seeding causes the period of the PRNG to decrease because you jump to a new point within the sequence. That in itself reduces the amount of randomness.
If re-seeding made it seem less random then that was just dumb luck. It can just as easily make things worse.
The problem you had, with seeing a pattern in the output, is correctly solved by using a better PRNG.

To the OP: It's a common misconception, but the method you are using isn't actually any less biased that just using the modulus operator, it just puts the bias in different places instead of on values at one end of the range. Specific values still always have a slightly higher probability.
Yes you are using the higher bits of the random value, which is good because these are generally more random, but that is still tangential to the issue of the distribution bias introduced by the funnelling of N inputs down to K outputs where gcd(K, N) != 1.

Of course for what you want to use it for, the amount of bias you get is probably not important.

However, if you want to see how to actually remove distribution bias, check out my post #6 here about using the rejection method:
http://cboard.cprogr...etween-x-y.html Edited by iMalc

##### Share on other sites

Unless you intentionally wish to repeat a previous random sequence, calling srand again is undesireable.
Re-seeding causes the period of the PRNG to decrease because you jump to a new point within the sequence. That in itself reduces the amount of randomness.
If re-seeding made it seem less random then that was just dumb luck. It can just as easily make things worse.
The problem you had, with seeing a pattern in the output, is correctly solved by using a better PRNG.

I stand corrected. The PRNG was the Macintosh library function, Random. It gave a 16-bit signed output and it was pretty terrible. After a few thousand iterations, the "randomness" would converge to a small sequence of repeating numbers. The only way to resolve this was to reseed the RNG. I've been doing it this way ever since, save for when I switched to using Mersanine twister.

Thanks for the clarification.