# Why do we need a seed in Random Number Generators?

I want to know how Random number generator(RNG) works in any PL(Programming language). I know the random methods has short periods. i.e they start repeating the values after specific number of time.

Seeds are used to initialise the random numbers generated by the RNG. IF any PL uses its own SEEDS, how specifying my seed will make any difference. I heard people saying that specifying the seed help in better control over the sequence Generated. I don't understand it as a whole. Could any one please help. with the same,

• " I know the random methods has short periods" - the opposite is the case, the periods are typically not short. Moreover, did you read the Wikipedia article about this topic? Commented Sep 30, 2015 at 5:41
• For a concrete example, see The C Programming Language, chapter 2. In that chapter is a simple implementation of srand (for seeding) and rand (for generating the next number in the sequence). It is C specific but you could apply it to any programming language. Commented Sep 30, 2015 at 10:35
• It is not a random number generator, but a pseudo-random number generator; the "pseudo" is a very important adjective. Commented Feb 10, 2016 at 7:53

I want to know how Random number generator(RNG) works in any PL(Programming language).

Languages tend to have their own implementations of pseudo-random number generators, so you cannot know how a pseudo-random number generator works in any programming language. Also, there is not much value that we could add here besides pointing you to the relevant article in Wikipedia.

The simplest approach, however, which produces fairly good results and is very fast, is to keep track of the last integer issued, and each time a new pseudo-random integer is requested, increment it by one and then multiply it by a huge prime constant before returning it. This multiplication usually results in substantial overflow, which is ignored. The effect is that the resulting bit pattern is for all practical purposes random. The increment by one is necessary in order to ensure that if the number ever reaches zero, subsequent multiplications will not keep yielding zero.

I know the random methods has short periods. i.e they start repeating the values after specific number of time.

I do not know how you got the impression that pseudo-random numbers have short periods, because this is simply not true. Pseudo-random number generators tend to have huge periods.

Seeds are used to initialise the random numbers generated by the RNG. IF any PL uses its own SEEDS, how specifying my seed will make any difference.

A pseudo-random number generator will use its own seed only if you do not specify your own seed. If you specify your own seed, then the pseudo-random number generator will use your seed.

I heard people saying that specifying the seed help in better control over the sequence Generated. I don't understand it as a whole. Could any one please help. with the same,

### Do try this at home:

Write a program which draws a starry sky and then clears it, using only `GetPixel()` and `SetPixel()`.

So, what you have to do is write a loop which generates, say, 1000 pseudo-random pairs of coordinates, and inverts each pixel at those coordinates. If the pixel was black, it makes it white. If it was white, it makes it black. If the screen was initially all black, about 1000 white stars should appear.

Now, to clear the stars, you could clear the entire screen, but there is a cooler way to do it.

Initialize your pseudo-random number generator with the exact same seed, and then re-apply the above loop. The exact same sequence of pseudo-random numbers will be generated, therefore the exact same set of coordinates will be visited, so each one of those pixels will be re-inverted, resulting in a black sky again.

• `Languages tend to have their own implementations of pseudo-random number generators, so you cannot know how a pseudo-random number generator works in any programming language.` On the contrary, if you can't know how [insert feature here] works in any language, there's something very wrong with that language and you probably shouldn't be using it. Commented Sep 30, 2015 at 16:03
• @MasonWheeler the question, as stated, implies that all programming languages have the same mechanism, and therefore it is possible to know how it works in any language. (I take the word "any" here as being semantically close to "all".) And that, you cannot do. Because they do not all have the same mechanism. Commented Sep 30, 2015 at 16:47
• @MasonWheeler As a matter of fact, it is not even a language feature, it is a feature of the runtime, so even with the same language two different runtimes may have different implementations. Commented Sep 30, 2015 at 16:48
• I would actually call that two different languages, since the most reasonable definition of "a programming language" is that it's defined by its compiler and standard libraries, including the runtime. Change either one in a non-trivial way, and you have a different language that will be used in a different way. (Who can doubt, for example, that C# 2 with generics is a fundamentally different creature than C# 1?) Therefore, the RNG implementation is a part of the language. Commented Sep 30, 2015 at 17:22
• @MasonWheeler well, sorry, but I beg to differ on both accounts. `rand()` promises a pseudo-random number, without making any promises as to how it is implemented, and every different standard library out there comes with its own implementation of `rand()` which delivers pseudo-random numbers. That does not constitute a non-trivial change, nor a trivial change, nor even a change. It is to spec. Also, if a language is backwards compatible with its previous version, it is still the same language, de facto. Commented Sep 30, 2015 at 17:39

Let's say that you wrote an encryption algorithm that uses a pseudo-random sequence of numbers to generate the encrypted content. Your encryption function works two ways; you can encrypt using a random number sequence, and then decrypt using the same random number sequence.

Wouldn't it be nice if you could later specify the same seed so that you could get the same set of numbers to decrypt with? That's why we have seeds; to produce the same sequence of "random" numbers.

Of course, if you just need any sequence of random numbers, you can use a semi-random seed like the millisecond digits of the current time.

• havery , Thanks, apart from control over output, do we have any other benefit of using the seed? Commented Sep 30, 2015 at 7:38
• See here for more detail about the advantages of seeds. Commented Sep 30, 2015 at 7:48
• so its a way to generate a random number and be able to "remember" what it was without "saving it"?? Commented Jul 19, 2018 at 6:04
• @TimBoland: The point is not to save a sequence of random numbers, it's that you can algorithmically generate the same sequence of random numbers if both receiver and sender have the same key. Presumably an attacker does not also possess the key. Commented Jul 19, 2018 at 15:01