why is it when i call srand() at 2 very different points it cause numbers to not be random? Once i remove one of them it goes back to normal.
-
3You'll have to show some code demonstrating what you mean, and also describe what you mean by "not random". `rand()` is a *pseudo*-random number generator; the sequence of numbers it generates is entirely determined by the most recent call to `srand()`. – Sneftel Feb 12 '14 at 11:18
4 Answers
It depends on how you call it. The purpose of srand()
is to seed the pseudo-random number generator used by rand()
. So when you call srand(i)
, it will initialise rand()
to a fixed sequence which depends on i
. So when you re-seed with the same seed, you start getting the same sequence.
The most common use case is to seed the generator just once, and with a suitable "random" value (such as the idiomatic time(NULL)
). This guarantees makes it likely that you'll get different sequences of pseudo-random numbers in different program executions.
However, occasionally you might want to make the pseudo-random sequence "replayable." Imagine you're testing several sorting algorithms on random data. To get fair comparisons, you should test each algorithm on the exact same data - so you'll re-seed the generator with the same seed before each run.
In other words: if you want the numbers simply pseudo-random, seed once, and with a value as random as possible. If you want some control & replayability, seed as necessary.

- 167,307
- 17
- 350
- 455
srand (seed);
Two different initializations with the same seed will generate the same succession of results in subsequent calls to rand.
If seed is set to 1, the generator is reinitialized to its initial value and produces the same values as before any call to rand or srand.
Each time rand() is seeded with srand(), it must produce the same sequence of values.

- 29,204
- 9
- 82
- 118
Are you initializing the srand? You have to initialize it in the beginning of you function/code like this:
srand(time(NULL));
It should work :)

- 74
- 2
- 3
- 15
-
1And what do you think happens when you then call `srand(time(NULL));` later in the same program, soon enough that `time(NULL)` returns the exact same value as the first time? – Feb 12 '14 at 11:26
-
Well, eventually it will but i have assumed that his program is not a very complex one. – falkon21 Feb 12 '14 at 11:31
-
"Eventually"? I wasn't referring to the counter overflowing after days, I was referring to calling `time(NULL)` twice in the same second (or whatever other unit `time_t` is in for the particular system). Especially if the program is not a complex one, the full run of the program doesn't even need to take a second. – Feb 12 '14 at 11:35
-
Could you please explain me what are you trying to say? My awnser was as we can see what he wanted. It was just the initialization of rand(). – falkon21 Feb 12 '14 at 11:43
-
What I'm trying to say is this: the question already stated that `srand` *was* being called. It was being called twice. Now, suppose you're calling `srand(time(NULL))`, then `rand()`, then `srand(time(NULL))`, then `rand()` again. This is likely to give the same "random" number twice, because `time(NULL)` is likely to give the same value twice. Because of that, adding yet another `srand(time(NULL))` won't benefit the OP. – Feb 12 '14 at 11:57
-
Ok, but i didn't said to use 2 times the function srand(time(NULL)). It should only be used once in the beginning of the function/program for initialization. – falkon21 Feb 12 '14 at 12:15
You may read about pseudo random numbers generators, standard library srand-rand functions are implementation of one of them. The core idea is that pseudo random generator is initialized with the special number - seed. srand() is used to set seed. For every seed pseudo random generator generate exactly the same sequence of numbers ever. By using different seeds you'll get different sequences of numbers. So if you want to get different random numbers everytime you start you program, you need everytime to set new seed. The one of simpliest way to do this is to use time for seed.
#include <time.h>
srand((unsigned int)time(0));

- 3,120
- 3
- 22
- 42