Jump to content

  • Log In with Google      Sign In   
  • Create Account


RandNumber problem


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 noatom   Members   -  Reputation: 767

Like
0Likes
Like

Posted 04 November 2012 - 11:36 AM

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?

Sponsor:

#2 Álvaro   Crossbones+   -  Reputation: 11861

Like
5Likes
Like

Posted 04 November 2012 - 11:38 AM

You should only call srand once at the beginning of the program, and never again. You are calling it before each call to rand, which is not what you are supposed to do.

#3 noatom   Members   -  Reputation: 767

Like
0Likes
Like

Posted 04 November 2012 - 11:47 AM

it worked,thanks a lot!

#4 MarkS   Prime Members   -  Reputation: 875

Like
0Likes
Like

Posted 05 November 2012 - 12:36 AM

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.

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

#5 iMalc   Crossbones+   -  Reputation: 2258

Like
3Likes
Like

Posted 05 November 2012 - 02:31 AM

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.

[edit]
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, 05 November 2012 - 12:23 PM.

"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms

#6 MarkS   Prime Members   -  Reputation: 875

Like
0Likes
Like

Posted 05 November 2012 - 03:17 AM

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.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS