Realy weird random number generator bug

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jul 2007
Posts: 47
Reputation: Arctic wolf is an unknown quantity at this point 
Solved Threads: 1
Arctic wolf Arctic wolf is offline Offline
Light Poster

Realy weird random number generator bug

 
0
  #1
Aug 1st, 2007
Hello,
I made a program that uses a random number generator,
my generator looks like this:
  1. void randomize ()
  2. {
  3. srand((unsigned)time(NULL));
  4. }
  5.  
  6. int random (int highest)
  7. {
  8.  
  9. int randomnum;
  10.  
  11. randomnum= int(highest*rand()/(RAND_MAX+1.0));
  12. return randomnum;
  13. }
here is the piece of code that calls the generator:
  1. int first,second;
  2. randomize();
  3. first=random(8);
  4. second=random(11);
the weird thing is that the second call works well and gives a random number every time,but the first doesn't.
It gives the same number every time,and that number always equals
the function parameter(int highest) minus 1(when the parameter is 8
the function returns 7,when I changed it to 10 the function returned 9 etc.).
Can anyone explain why does it happen?

p.s.
I'm using VC++ 6.0
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Realy weird random number generator bug

 
0
  #2
Aug 1st, 2007
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Realy weird random number generator bug

 
0
  #3
Aug 1st, 2007
Because rand() isn't random at all perhaps?

The default algorithm is very simple, something along the lines of
http://en.wikipedia.org/wiki/Linear_...tial_generator

The first few iterations are likely to exhibit a degree of predictability which one might not expect.

See also http://c-faq.com/lib/notveryrand.html
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 47
Reputation: Arctic wolf is an unknown quantity at this point 
Solved Threads: 1
Arctic wolf Arctic wolf is offline Offline
Light Poster

Re: Realy weird random number generator bug

 
0
  #4
Aug 2nd, 2007
Hello Salem,
I know that rand() is a pseudorandom.
You probably missed this part of my program:
  1. void randomize ()
  2. {
  3. srand((unsigned)time(NULL));
  4. }
  5. .
  6. .
  7. .
  8. .
  9. randomize();

as you see I seed the generator with current time,therfore
the generator should start with a different number every time,
and the problem I described shouldn't happen.
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 487
Reputation: Bench has a spectacular aura about Bench has a spectacular aura about Bench has a spectacular aura about 
Solved Threads: 48
Bench's Avatar
Bench Bench is online now Online
Posting Pro in Training

Re: Realy weird random number generator bug

 
0
  #5
Aug 2nd, 2007
Originally Posted by Arctic wolf View Post
Hello Salem,
I know that rand() is a pseudorandom.
You probably missed this part of my program:
  1. void randomize ()
  2. {
  3. srand((unsigned)time(NULL));
  4. }
  5. .
  6. .
  7. .
  8. .
  9. randomize();

as you see I seed the generator with current time,therfore
the generator should start with a different number every time,
and the problem I described shouldn't happen.
You shouldn't call srand more than once in the lifetime of your program. Perhaps this is why you're getting repetition?

Also, time isn't exactly the best seed for srand. Its common for the first number to always be the same when seeding with the current time. you could try a different method of seeding the generator, such as the one suggested by Narue here -
http://www.eternallyconfuzzled.com/a..._art_rand.aspx
Last edited by Bench; Aug 2nd, 2007 at 10:09 am.
¿umop apisdn upside down?
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Realy weird random number generator bug

 
0
  #6
Aug 2nd, 2007
> the generator should start with a different number every time,
And if the implementation of rand() just uses the most significant 16 bits of the 32 bit number, that as far as it is concerned, time() is a constant pretty much throughout the day.

So it can take a couple of iterations for all the rapidly changing least significant bits to have an impact on the pseudo random sequence.

If you really want to know, run the code in the debugger and then single step into the srand() and rand() calls in asm mode and figure out what it is doing.
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 47
Reputation: Arctic wolf is an unknown quantity at this point 
Solved Threads: 1
Arctic wolf Arctic wolf is offline Offline
Light Poster

Re: Realy weird random number generator bug

 
0
  #7
Aug 2nd, 2007
Thanks everybody.
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 47
Reputation: Arctic wolf is an unknown quantity at this point 
Solved Threads: 1
Arctic wolf Arctic wolf is offline Offline
Light Poster

Re: Realy weird random number generator bug

 
0
  #8
Aug 2nd, 2007
Thanks again for all the help,
just wanted to ask one more question:
In the link that Bench posted(http://www.eternallyconfuzzled.com/a..._art_rand.aspx) there is a piece of code that looks like this:
  1. for ( i = 0; i < sizeof now; i++ )
  2. {
  3. seed = seed * ( UCHAR_MAX + 2U ) + p[i];
  4. return seed;
  5. }
I just wanted to ask what meaning the "U" after the "2" has?
(I guess it means "unsigned",but why does it matter,why just not write:
UCHAR_MAX+2)
Last edited by Arctic wolf; Aug 2nd, 2007 at 10:09 pm.
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 487
Reputation: Bench has a spectacular aura about Bench has a spectacular aura about Bench has a spectacular aura about 
Solved Threads: 48
Bench's Avatar
Bench Bench is online now Online
Posting Pro in Training

Re: Realy weird random number generator bug

 
0
  #9
Aug 2nd, 2007
I believe omitting the 'U' from the expression causes a warning to be thrown up when compiling with warnings at their highest level. Otherwise, its not a problem.
Last edited by Bench; Aug 2nd, 2007 at 10:26 pm.
¿umop apisdn upside down?
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC