.:: Bots United ::.  
filebase forums discord server github wiki web
cubebot epodbot fritzbot gravebot grogbot hpbbot ivpbot jkbotti joebot
meanmod podbotmm racc rcbot realbot sandbot shrikebot soulfathermaps yapb

Go Back   .:: Bots United ::. > Developer's Farm > General Programming
General Programming Help others and get yourself helped here!

Reply
 
Thread Tools
srand() aberration ???
Old
  (#1)
Pierre-Marie Baty
Roi de France
 
Pierre-Marie Baty's Avatar
 
Status: Offline
Posts: 5,049
Join Date: Nov 2003
Location: 46°43'60N 0°43'0W 0.187A
Default srand() aberration ??? - 14-06-2004

well well, I feel kinda n00b here but something is puzzling me.

If I do
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
 
void main (void)
{
   srand (time (NULL)); // initialize random seed
 
   // generate a pseudo-random number ranging from 0 to 99
   printf ("%d\n", (int) (100 * (float) rand () / (RAND_MAX + 1)));
}
and I run my application, I get the same number each time I run it.

Now if I do
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
 
void main (void)
{
   srand (time (NULL)); // initialize random seed
 
   for (i = 0; i < 100; i++)
	  rand (); // call rand a couple times 
 
   // generate a pseudo-random number ranging from 0 to 99
   printf ("%d\n", (int) (100 * (float) rand () / (RAND_MAX + 1)));
}
Now I get a different number each time I run it (which is what I expected initially).

But could someone tell me WHY on Earth I need to call rand() a great number of times before using it really to have a random number ? Or put it differently why is it ALWAYS the same numbers that pop up during the first rand() calls, and this NO MATTER what I put in the seed ?

Any explanation ???



RACC home - Bots-United: beer, babies & bots (especially the latter)
"Learn to think by yourself, else others will do it for you."
  
Reply With Quote
Re: srand() aberration ???
Old
  (#2)
botman
Super Moderator
 
Status: Offline
Posts: 280
Join Date: Jan 2004
Location: Plano, TX
Default Re: srand() aberration ??? - 14-06-2004

I think you have something messed up somewhere. This code generates the following output on my machine...
Code:
#include <stdlib.h>
 #include <stdio.h>
 #include <time.h>
 
 void main(void)
 {
    srand((unsigned int)time(NULL));
 
    for (int i=0; i < 10; i++)
 	  printf("rand = %d\n", rand() % 100);
 }
C:\TEMP>cl x.cpp
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 12.00.8804 for 80x86
Copyright (C) Microsoft Corp 1984-1998. All rights reserved.

x.cpp
Microsoft (R) Incremental Linker Version 6.00.8447
Copyright (C) Microsoft Corp 1992-1998. All rights reserved.

/out.exe
x.obj

C:\TEMP>x
rand = 15
rand = 90
rand = 54
rand = 10
rand = 29
rand = 18
rand = 29
rand = 70
rand = 83
rand = 56

C:\TEMP>x
rand = 18
rand = 38
rand = 18
rand = 73
rand = 44
rand = 45
rand = 42
rand = 43
rand = 94
rand = 8

C:\TEMP>x
rand = 21
rand = 19
rand = 14
rand = 69
rand = 90
rand = 72
rand = 54
rand = 16
rand = 38
rand = 60

botman
  
Reply With Quote
Re: srand() aberration ???
Old
  (#3)
Rick
Council Member
 
Rick's Avatar
 
Status: Offline
Posts: 690
Join Date: Dec 2003
Location: Holland
Default Re: srand() aberration ??? - 14-06-2004

heh weird stuff I tried this:
Code:
 #include <stdio.h>
 #include <stdlib.h>
 #include <time.h>
 
 void main (void)
 {
    srand (time (NULL)); // initialize random seed
 
    // generate a pseudo-random number ranging from 0 to 99
    for (int i=0;i<15;i++)
 	   printf ("%d\n", (int) (100.0f * (float) rand () / float((RAND_MAX + 1.0f))));
 
    system("pause");
 }
and the first number was ALWAYS the same, the other numbers where random though.
  
Reply With Quote
Re: srand() aberration ???
Old
  (#4)
Pierre-Marie Baty
Roi de France
 
Pierre-Marie Baty's Avatar
 
Status: Offline
Posts: 5,049
Join Date: Nov 2003
Location: 46°43'60N 0°43'0W 0.187A
Default Re: srand() aberration ??? - 14-06-2004

that's what I have here too. The first numbers are ALWAYS the same.

But now, even more weird: I notice that instead of doing
Code:
srand (time (NULL)); // initialize random seed
if I do
Code:
srand (GetTickCount ()); // initialize random seed
instead, then it does work normally (i.e, the first numbers are different this time).

What the ... ???

@botman
I might be wrong, but I think doing
Code:
rand () % 100
ends up with a not so random pseudo-random number generation. Doing a modulo unevens the distribution of the numbers in the suite along the {0, 100} axis. Any math brute (Hey Joe!) to confirm ?



RACC home - Bots-United: beer, babies & bots (especially the latter)
"Learn to think by yourself, else others will do it for you."

Last edited by Pierre-Marie Baty; 14-06-2004 at 14:30..
  
Reply With Quote
Re: srand() aberration ???
Old
  (#5)
@$3.1415rin
Council Member, Author of JoeBOT
 
@$3.1415rin's Avatar
 
Status: Offline
Posts: 1,381
Join Date: Nov 2003
Location: Germany
Default Re: srand() aberration ??? - 14-06-2004

yep, doing a modulo isnt good since there are then numbers ( 65537 e.g. when the biggest is 65538 ) which are not possible, therefore not all values inside 0-100 will have the same probability. Although it's not a big deal when having RAND_MAX somewhere at 2^16 and the modulo @100


  
Reply With Quote
Re: srand() aberration ???
Old
  (#6)
KickBot
Member
 
Status: Offline
Posts: 17
Join Date: Apr 2004
Default Re: srand() aberration ??? - 18-06-2004

Tried this, to display rand() before the float roundup:

Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main (void)
{
srand (time (NULL)); // initialize random seed
// generate a pseudo-random number ranging from 0 to 99
unsigned int r = rand ();
printf("r=%u\n", r);
printf ("%d\n", (int) (100 * (float) r / (RAND_MAX + 1)));
 
system("pause");
 
return 0;
}
Some observations:

1) On successive run rand() seems to give *very close values*, so this explains why when you do "(int) (100 * (float) r / (RAND_MAX + 1)))" it gives the same value because the "r" values close to each other (this is a bad!). That explains why botman test does not show this behavior (no roundup).

2) Also time() give time in seconds, so if you run the test in the same second you'll have the same values because the seed is the same. That explains why GetTickCount() works better.

So it seems to make sure rand() doesn't start with close numbers you have to make sure the seeds are not close to each other, so GetTickCount() is a better candidate than time(). Bad rand. 9_9

Last edited by KickBot; 18-06-2004 at 12:05..
  
Reply With Quote
Re: srand() aberration ???
Old
  (#7)
@$3.1415rin
Council Member, Author of JoeBOT
 
@$3.1415rin's Avatar
 
Status: Offline
Posts: 1,381
Join Date: Nov 2003
Location: Germany
Default Re: srand() aberration ??? - 18-06-2004

well, but time() is seconds since 1900 or similar, so starting in the same second of a minute should make much difference ...

another possibility would be initializing with the lower 32 bits of the 64 bit counter in newer x86 cpu's running at cpu speed


  
Reply With Quote
Re: srand() aberration ???
Old
  (#8)
botmeister
Ex-Council Member
 
botmeister's Avatar
 
Status: Offline
Posts: 1,090
Join Date: Nov 2003
Location: Canada
Default Re: srand() aberration ??? - 19-06-2004

rand() is generally considered as a poor random number generator.

You can use this instead, and it generates a 32 bit result instead of 16 bit
Code:
unsigned long idnum;
unsigned long lrand( )
{
   idnum = 1664525L * idnum + 1013904223L;
   return idnum;
}


Maker of the (mEAn) Bot.Admin Manager

"In theory, there is no difference between theory and practice. But, in practice, there is." - Jan L.A. van de Snepscheut
  
Reply With Quote
Re: srand() aberration ???
Old
  (#9)
sfx1999
Member
 
sfx1999's Avatar
 
Status: Offline
Posts: 534
Join Date: Jan 2004
Location: Pittsburgh, PA, USA
Default Re: srand() aberration ??? - 20-06-2004

Why are you calling rand within srand()? Isn't srand supposed to be a seeded RNG? Why do you have it dependant on rand()?
  
Reply With Quote
Re: srand() aberration ???
Old
  (#10)
Pierre-Marie Baty
Roi de France
 
Pierre-Marie Baty's Avatar
 
Status: Offline
Posts: 5,049
Join Date: Nov 2003
Location: 46°43'60N 0°43'0W 0.187A
Default Re: srand() aberration ??? - 20-06-2004

Because you HAVE to call srand() at least once if you intend to use rand()... else each time you run the program you'll have the same series of numbers.

rand() is based on a math suite. Not sure its the real word. It's just like the math quizs for children : given the number you have, and the number before, and the number before it, deduce the number that's going to come right after. rand() is just that. If you don't initialize the seed (that is, the initial number of the suite) the output values will always be the same.

hrm, I should go to bed. sorry. 8am here.



RACC home - Bots-United: beer, babies & bots (especially the latter)
"Learn to think by yourself, else others will do it for you."
  
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump



Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
vBulletin Skin developed by: vBStyles.com