From what I’ve read on the c++ website, the srand function, puts a seed in the rand function, but I don’t really understand what a seed is.
-
2I would have a start with your research [here](https://en.wikipedia.org/wiki/Pseudorandom_number_generator) – NathanOliver Jul 26 '19 at 16:46
-
1Run a Google search for "pseudo-random number generation", and you should find plenty of information. – Sam Varshavchik Jul 26 '19 at 16:46
-
1Also, [this](https://en.wikipedia.org/wiki/Random_seed). – Sufian Latif Jul 26 '19 at 16:47
-
2What is "the c++ website"? It may be explained on the website you're looking at. – Drew Dormann Jul 26 '19 at 16:47
-
Also, https://stackoverflow.com/questions/22639587/random-seed-what-does-it-do – Drew Dormann Jul 26 '19 at 16:48
-
@DrewDormann Should we add it to the dupe list? – NathanOliver Jul 26 '19 at 16:49
-
I found a better dupe that answers this question exactly. – Sam Varshavchik Jul 26 '19 at 16:50
-
@NathanOliver yours is C, mine is Python... I suppose so. – Drew Dormann Jul 26 '19 at 16:51
-
There, now it has all the targets :) – NathanOliver Jul 26 '19 at 16:54
-
1@SamVarshavchik Not really; that question is about how `rand()` "knows" what the seed is when there's no connection [at the callsite] between `rand()` and `srand()` and thus no evident transfer of state (obvs it's a global somewhere). This one is just... what is a seed – Lightness Races in Orbit Jul 26 '19 at 16:57
-
@LightnessRacesinOrbit *"but I don’t really understand what a seed is."* – Aykhan Hagverdili Jul 26 '19 at 17:01
-
1[rand() Considered Harmful](https://channel9.msdn.com/Events/GoingNative/2013/rand-Considered-Harmful). You *really* should consider using the modern facilities in the [
](https://en.cppreference.com/w/cpp/numeric/random) header. – Jesper Juhl Jul 26 '19 at 17:02 -
I really don't get the down votes. Sure, it's not a *great* question, but it's a fair enough question; "what's a seed in the context of a PRNG". And it's quite answerable. – Jesper Juhl Jul 26 '19 at 17:25
-
@Ayxan Exactly. – Lightness Races in Orbit Jul 26 '19 at 17:26
-
@JesperJuhl Lack of research evidence is quite, well, evident – Lightness Races in Orbit Jul 26 '19 at 17:26
-
1@LightnessRacesinOrbit Yes it is. No argument there. But it's still answerable (see my proposed answer for example) and I think it makes more sense to answer it than close it. This is not the first, nor last, newbie to be confused about what a PRNG seed is. So we may as well have an answer. – Jesper Juhl Jul 26 '19 at 17:27
-
@JesperJuhl Nobody's talking about answering or closing. We're talking about downvoting. If you hover over the downvote button you'll see the answer to your _"I really don't get the down votes"_. As for answering, yes of course it _can_ be answered; the bigger question is whether it _should_ be. – Lightness Races in Orbit Jul 28 '19 at 00:03
1 Answers
"I don’t really understand what a seed is" - A seed in the context of a Pseudo Random Number Generator (PRNG) is a starting value to use for the generation of the pseudo random sequence.
A PRNG that starts with the same seed will (in most cases) produce the same sequence of random numbers. This is great for replayability of a scenario, but is not generally what you want in production. Usually you want to provide a unique/unpredictable seed to the generator for each instance of the application, so that each run gives a unique stream of pseudo random numbers.
I would recommend using the new random facilities (since C++11) that are available in the random header over srand()
/rand()
since rand
has a very low range and usually also a very low period. The new facilities are much better.
You may also find this talk enlightening: rand() Considered Harmful
The Wikipedia page on pseudo random number generators is also worth a read.

- 30,449
- 3
- 47
- 70