Programming – Why a Seed is Needed in Random Number Generators

programming-languagesrandom

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,

Best Answer

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.

Related Topic