# Thread: Random number generator/srand

1. ## Random number generator/srand

Hi.

Im writing a program to do a random walk simulation. In essence it is a random number generator. I'm seeding from the system clock before decalring a loop using the rand() generator.

My question is....

If im seeding from the system clock, shouldnt the seed be different each time the program is compilled?

Here's how I innisiate the seed (srand) using the time.h library:

time_t curr_time;

srand( (unsigned) time(&curr_time));

Basically I'm defining the system time to be a variable called curr_time then assign the seed to be that value. If that is true srand should be different everytime the program is compilled but i have found it is not YET........the rand function is.........Im stumped.

Any help much appreciated

2. Try your seed as simply srand(time(0)); That will give you a different seed value every second.
Call it once at the beginning of your program and use rand() after.

Code:
```int main(void);
{
int i;
srand(time(0));

for (1 = 0; i < 100; i++)
printf("%d   ", rand() % 100);

return 0;
}```

3. Make sure you included <time.h>, <stdlib.h> so that time returns the proper type.

4. Hi guys,

thanks for the replies. I have the time.h and stdlib.h library's in there and I tried the srand(time(0)); suggestion. It returns the same value as when I used my method and each run of the loop.....confusing!!

I know it is itterating as the rand() is different each time but cant figure out why it wont print a different srand at each stage.

5. Try this fixed version of CommonTater's example:
Code:
```#include <stdlib.h>
#include <stdio.h>

int main(void)
{
int i;
srand(time(0));

for (i = 0; i < 100; i++)
printf("%d ", rand() % 100);

return 0;
}```
Also, read Prelude's tutorial on using rand().

6. Thanks Laserlight for your post but unfortuneatly it doesnt help.

Just to clarify, i have a fully functioning program that is returning the correct values. There appears to be nothing wrong with my program except.......

when I print the seed it doesnt change BUT the random number does. This is suggesting that the seed is indeed changing but the print function some how wont when I compile. I've checked the system time using C and it is changing.

Ive also noticed one other strange thing. Depending on where in the program I place the printf statement the sedd is different but remains unchanged on each compile of the program.

Maybe I can submit my program for review. Not sure how to do it though....I assume between [QUOTE][QUOTE].....?

7. time() is called at run time, not compile time.

Recompiling the program will not affect the value returned - the system clock determines that (at least, within the granularity of the time() function - two calls close together may produce the same result).

8. Hi Grumpy,

Thanks, This sounds like a winner...I dont know the difference between run and compile time but ill check..........but if this is true and i print system time and seed side by side...both should change not just the system time?.........this is whats happening

9. Originally Posted by Dave.b
Maybe I can submit my program for review. Not sure how to do it though....I assume between [quote][quote].....?
Not quite: between [code][/code] bbcode tags.

Originally Posted by Dave.b
but if this is true and i print system time and seed side by side...both should change not just the system time?
You are directly using the return value of time(0) as the seed, so it really is the same thing. In your original code, the curr_time variable is useless: all it does is store the result of the call to time(), but you are already directly using that result as an argument to srand().

Did you read Prelude's tutorial?

10. Thanks laserlight,

Yup I read the tutorial but It didnt answer my question or else it did and i dont get it.......

You are directly using the return value of time(0) as the seed, so it really is the same thing.
Would you mind elaborating on this please? It seems to me that yes it will be the same except for when the program is compilled again... (sorry grumpy still havnt got that compile run thing sorted)...as the system clock will have changed by then?

As for:

In your original code, the curr_time variable is useless: all it does is store the result of the call to time(), but you are already directly using that result as an argument to srand().
Thats true, just good practice where im doing this for.

Thanks again

11. Originally Posted by Dave.b
Would you mind elaborating on this please?
The argument to srand is the seed for the pseudorandom number generator, e.g., if you call srand(1); the seed will be 1.

Originally Posted by Dave.b
It seems to me that yes it will be the same except for when the program is compilled again... (sorry grumpy still havnt got that compile run thing sorted)...as the system clock will have changed by then?
Well, here is a test program:
Code:
```#include <stdio.h>
#include <time.h>

int main(void)
{
printf("%lu\n", (unsigned long)time(0));
return 0;
}```
Compile this program and run it once. Record the output. Make yourself a cup of coffee and finish drinking it. Without compiling it again, run the program again. Is the output the same? Now, go for a jog and take a shower when you return. After the shower, without compiling your program again, run it. Is the output the same?

Popular pages Recent additions