I don't get the purpose of the seed in the srand
function in C. Can someone explain that to me? I know that srand
is used to generate random numbers each time the program is run (unlike rand()
which generates the same random numbers each time).

- 30,332
- 17
- 55
- 95

- 35
- 1
-
2It is a pseudo random number generator, which means that it is deterministic. `srand(seed)` function initializes the RNG with a "starting value" and `rand()` generates a random number of it. The reason you initialize it mostly with the time value is that you get different numbers at different times of execution. – Osiris Aug 20 '18 at 17:38
-
@Osiris What does RNG mean? – user10251257 Aug 20 '18 at 17:40
-
Its just an acronym for "Random Number Generator" – Osiris Aug 20 '18 at 17:40
-
@Osiris How can I make a program to generate a random number between a group of different numbers? – user10251257 Aug 20 '18 at 17:42
-
You can take a look at [this](https://stackoverflow.com/questions/1202687/how-do-i-get-a-specific-range-of-numbers-from-rand) question. – Osiris Aug 20 '18 at 17:44
-
@Osiris The numbers I have are not consecutive tho. For example I have a group of numbers composed by 2, 4, 6, 8 and 10. How can I generate a random number between these ones? – user10251257 Aug 20 '18 at 17:46
-
2You can make an array of these numbers and generate a random index from `0` to `length-1`. If you have additional question which differ from your original one you should make a new question rather then asking in the comments, because otherwise the question becomes unclear. – Osiris Aug 20 '18 at 17:49
-
Using a pre-defined seed for a pseudo random number generator is especially helpful while you're debugging since when you use the same seed, you will generate the same sequence of `random numbers` during each run. Then when you deploy, you can switch from a pre-defined seed to something more random, like the current time – bruceg Aug 20 '18 at 18:16
-
1"I know that srand is used to generate random numbers " - No. You will find the description of `srand` in the documentation. This also explains the seed. – too honest for this site Aug 20 '18 at 18:39
4 Answers
The purpose of srand
and the seed passed to it is to give you control over the sequence generated by rand
.
srand
initializes the data used by rand
, so it determines what the sequence of numbers generated by rand
is. You can use this to choose whether to generate a new sequence (one you have not generated previously) or to repeat an old sequence (one you generated previously).
For example, suppose you have a simulation of some sort, and random numbers are desired to select some inputs to this simulation, such as when customers arrive or what particles enter the system. For this, you can simply use rand
repeatedly to generate numbers. When the simulation is done, you might want to run it again to see how the simulation varies due to effectively random changes in its input. However, if you run the simulation program again, it will behave exactly the same way, because, if srand
is not called, rand
always produces exactly the same sequence.
So, to generate a different sequence each time, a program may call srand
and pass it a different seed. Often, the current time, as returned by the standard time
routine, is used as a seed. However, any method of choosing a different value in different runs of the program will serve this purpose. (Note that time(NULL)
commonly returns a number of seconds, so executing a program that uses srand(time(NULL))
twice in quick succession will result in the same sequence of numbers being used if both executions of the program start within the same second.)
It does not matter which value is passed for the seed, just that it is different from other instances. The reason it does not matter is that a good random number generator uses complicated and well-designed functions to convert the seed into the state of the random number generator and to calculate the generated numbers, and this design makes it difficult for a choice of seed to have any deliberate effect on the generated numbers.
On the other hand, sometimes you want to repeat a sequence. Perhaps one run of the simulation did something interesting, and you want to study it in more detail. Or you want to provide the program to colleagues along with all the data needed to reproduce your results. Or a program has a bug that only manifests sometimes, so you need to exactly repeat execution while debugging. In these cases, you would pass the same seed to srand
in each execution in order to reproduce the same sequence of generated numbers.
In programs where I use a varying seed, as with time(NULL)
, I have the program write the seed it is using to output, so that, if the user later desires to run with the same sequence, they can pass that seed to the program with a command-line switch (also built into the program) specifying to use that seed.

- 195,579
- 13
- 168
- 312
-
This answer was quite useful to me. Just one question: shouldn't the argument of `time()` be a pointer to `time_t`? So instead of `srand(time())` it should be `srand(time(NULL))`? – builder-7000 Aug 22 '18 at 06:04
A random generator is often constructed so that it produces a new random number from the previous. It is a function of the previous number. This means that when the random generator generates a number the second time, you will get the exact same sequence again.
A common way to do it is this:
X_n+1 = (a*X_n + b) % m
X_n+1
is the next random number. X_n
is the previous, and a
, b
and m
are suitable constants.
Eventually you will hit a number you have used before. So you could look at it as you have a long circular chain of numbers. srand
determines where in this cycle to start. To ensure that you always get different numbers you often use the current time and date as seed.
Note that the above description is not necessarily 100% true for more sophisticated random generators, but the principle is the same.
There is another good use for srand
too. If you use the same seed you get the same random sequence, which can be very good when you are debugging or your code. Some bugs may only manifest themselves for certain random numbers.

- 30,332
- 17
- 55
- 95
-
This answer acknowledges the description it gives is not true for “more sophisticated” (pseudo) random number generators—so why have that part of the answer at all? It is not necessary to explain what the purpose of a seed is. Since the question asks what the **purpose** of a seed is, that should be addressed more directly. – Eric Postpischil Aug 20 '18 at 19:11
-
@EricPostpischil *"It is not necessary to explain what the purpose of a seed is. Since the question asks what the purpose of a seed is, that should be addressed more directly.*" - I'm sorry, but this seems like a contradiction. What do you mean? – klutt Aug 20 '18 at 19:53
-
The antecedent of “it” is “that part of the answer”. I recognize the sentence “It is not necessary to X” fits into idiomatic English meaning “X is not necessary,” but that was not the intended meaning in this case. The intended meaning is “That part of the answer is not needed for the goal of explaining what the purpose of a seed is.” In other words, to explain what the purpose of the seed is, we do not need that part of the answer—the first paragraphs, up to and including the one ending “the principle is the same,” are not needed. They could be removed or reduced to a few (correct) sentences. – Eric Postpischil Aug 20 '18 at 20:42
The rand()
function returns the next number in a sequence of number defined by some function (the random number generator), and the srand()
function defines what the first number in the sequence is.
If you pass a value to srand
, call rand
(for example) 5 times, then call srand
again with the same value and call rand
5 more times, the first set of 5 numbers will be exactly the same as the next set of 5 numbers.
If you don't call srand
, the effect is the same as calling srand(0)
, so that's why calling rand
in a program without calling srand
results in the same sequence of numbers. If you instead pass some varying value to srand
, such as the process ID, the current time, or some mathematical combination of them, you get a fairly unique starting point for your sequence of random numbers, leading to a fairly unique sequence.

- 205,898
- 23
- 218
- 273
-
(a) It is not incorrect that `srand` determines what the first number in the sequence is, but that leaves out the fact that it sets the entire state of the generator, which may be more than just what the next number is. (b) This does not speak well to the question asked, which is what is the **purpose** of the seed. While it does mention that the seed controls the sequence generated, it does not speak about **why** one would want to do that. – Eric Postpischil Aug 20 '18 at 19:14
It is an open question if real randomness exists in the universe, and very likely the answer is no. In informatics only pseudorandomness is used ( deterministic algorithms in RNGs ) it is generated i.e. by cascading of deterministic nonlinear dynamical systems / deterministic chaotic systems ( modulo is also a nonlinearity ). The seed
can be thought of as initial value of a deterministic chaotic system ( the RNG ) that then 'growth' by the sensitivity to initial conditions ( aka butterfly effect )
how the seed is acquired is another problem because it requires an external measurement that is then treated as random from the viewpoint of the code, if flow of time, or processor temperature is random from viewpoint of physics is another question :
Standard practice is to use the result of a call to
srand(time(0))
as the seed.
source : https://www.geeksforgeeks.org/rand-and-srand-in-ccpp/
Other possible sources of external 'randomness' beside time are temperatures i.e. of processor , ...

- 9,149
- 4
- 22
- 34
-
This, aside from being too abstract to be useful to beginners, does not address the question asked: What is the **purpose** of the seed? Merely stating it is an initial value does not explain why one would want to be able to set an initial value (as opposed to, say, having a fixed initial value). – Eric Postpischil Aug 20 '18 at 19:05
-
"It is an open question if real randomness exists in the universe, and very likely the answer is no." I agree that we don't know but is it really very likely? – Osiris Aug 21 '18 at 01:32
-
Is a philosophical questions and if you have a background in theory of nolinear dynamical systems it is clear that it is at least possible that there is no real randomness in the universe. There are hyperchaotic systems ( http://www.scholarpedia.org/article/Hyperchaos ) whichs output in the chaotic state is indistiguishable from 'random' noise, the same is valid for particular multifractal systems and also for simple chaotic maps ( https://ieeexplore.ieee.org/document/1236865/ ) – ralf htp Aug 21 '18 at 07:25
-
Multiplicative binomial cascades (multifractal) are based on Monte-Carlo simulations and MCS are based on pseudorandomness again (https://en.wikipedia.org/wiki/Multiplicative_cascade) . The key point is that multifractlity can be found virtually anywhere in nature. Basically taking the point that only pseudorandmoness exists and no real randomness is shifting the problem from an ontological into an epistemical. The ontological randomness is replaced by the butterfly effect problem that states that a system is unpredictable because the initial value can not be measured with sufficient accuracy – ralf htp Aug 21 '18 at 07:36