.:: Bots United ::.

.:: Bots United ::. (http://forums.bots-united.com/index.php)
-   General Programming (http://forums.bots-united.com/forumdisplay.php?f=25)
-   -   srand() aberration ??? (http://forums.bots-united.com/showthread.php?t=1968)

Pierre-Marie Baty 14-06-2004 06:04

srand() aberration ???
 
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 ???:(

botman 14-06-2004 13:52

Re: srand() aberration ???
 
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:x.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

Rick 14-06-2004 14:12

Re: srand() aberration ???
 
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.

Pierre-Marie Baty 14-06-2004 14:29

Re: srand() aberration ???
 
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 ?

@$3.1415rin 14-06-2004 15:18

Re: srand() aberration ???
 
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

KickBot 18-06-2004 12:02

Re: srand() aberration ???
 
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

@$3.1415rin 18-06-2004 16:34

Re: srand() aberration ???
 
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

botmeister 19-06-2004 07:37

Re: srand() aberration ???
 
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;
}


sfx1999 20-06-2004 04:32

Re: srand() aberration ???
 
Why are you calling rand within srand()? Isn't srand supposed to be a seeded RNG? Why do you have it dependant on rand()?

Pierre-Marie Baty 20-06-2004 07:57

Re: srand() aberration ???
 
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.


All times are GMT +2. The time now is 03:12.

Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.