How does the seed influence the generation of random numbers?

5

The question is quite simple, I would like to know how the System.Random class generates random pseudorandom numbers from a seed, which I think is rather strange. I know they are not totally random, and so I have my doubt as to how they are actually generated. For example,

int x = new Random(123).Next(1, 100);

x will have a random value, but if I always use that seed, it will not be random anymore and therefore, that seed will always return the same value in methods Next , NextDouble and NextBytes ? p>

If yes, what is the algorithm for creating these random numbers based on this seed?

Update

  

The center of my question is how random number is generated based on seed , not how that random number is generated . The focus of the question was to ask what and how the seed influence the generation of this number.

    
asked by anonymous 09.04.2017 / 06:59

1 answer

3

According to documentation : Pseudo-random numbers are chosen with equal probability from a finite set of numbers. The numbers chosen are not completely random because a mathematical algorithm is used to select them, but they are random enough for practical purposes. The current implementation of the Random class is based on a modified version of Donald E. Knuth's subtractive random number generator algorithm. For more information, see D. E. Knuth. The Art of Computer Programming, Volume 2: Seminumeric Algorithms. To generate a cryptographically secure random number, such as one that is suitable for creating a random password, use the RNGCryptoServiceProvider class or derive a class from System.Security.Cryptography.RandomNumberGenerator.

Basically, this class generates random numbers using as base another value, which would be the number of milliseconds that have passed since the operating system was started (Environment.TickCount). This is the default if you do not enter any value in the constructor.

If you pass a known number, then the values will always be the same.

Example:

var r = new Random(10);
Console.WriteLine(r.Next(1, 100)); //irá sempre retornar 95
Console.WriteLine(r.Next(1, 100)); //irá sempre retornar 75

r = new Random();
Console.WriteLine(r.Next(1, 100)); //retorno aleatório
Console.WriteLine(r.Next(1, 100)); //retorno aleatório

Source: link

    
10.04.2017 / 01:29