. NET we usually generate random numbers using the random class, and in some scenarios, I find that random numbers generated by randomness are unreliable, and in the following example we randomly generate 10 random numbers from a loop:
for (int0; i++) { new Random (); Console.WriteLine (random1. Next ()); }
Test generation is basically the same result at any time:
Obviously the above results are not reliable, why is this, because Microsoft's random class, found in C # generated random numbers using the algorithm is linear with congruential, this algorithm generates not absolute random, but a pseudo-random number, linear with the congruential algorithm of the formula is
: Number of n+1 = (Nth number * a + b)% M, the formula A, B and M are constants, is the factor that generates the random number, if you have never previously generated a random number by the same arbitrary object (that is, called the next method), then the nth random number will be specified as a default constant, This constant is specified by default when creating a random class, and random also provides a constructor that allows developers to use their own random number factor.
Some people say that the random random1 = new random (), to be placed outside the loop:
New Random (); for (int0; i++) { Console.WriteLine (random2). Next ()); }
The result of testing the above code execution is this:
It's not a reliable result.
Some say that using GUIDs produces fill factors:
for (int0; i++) { byte[] buffer = guid.newguid (). Tobytearray (); int 0 ); New Random (iSeed); Console.WriteLine (RANDOM3. Next ()); }
Test the results from the above code:
The results are still not reliable.
In order to generate a more reliable random number, Microsoft provides a class named System.Security.Cryptography.RNGCryptoServiceProvider under the System.Security.Cryptography namespace, which takes the system's current hardware information, Process information, thread information, system start time, and current exact time as fill factor, with better algorithms to generate high-quality random numbers, it is used as follows:
for(inti =0; I < -; i++) { byte[] Randombytes =New byte[8]; System.Security.Cryptography.RNGCryptoServiceProvider Rngserviceprovider=NewSystem.Security.Cryptography.RNGCryptoServiceProvider (); Rngserviceprovider.getbytes (randombytes); intresult = Bitconverter.toint32 (Randombytes,0); Result= System.Math.Abs (result);//Seek absolute valueConsole.WriteLine (Result); }
The test results were not found to be duplicated:
Summarize:
Random algorithm is simple, high performance, applicable to the situation of low randomness requirements, because rngcryptoserviceprovider in the generation of the need to query the above mentioned system factors, so the performance is slightly weaker than the random class, but the high quality and reliability is better. Which method to use depends on the situation
. NET generates random numbers using the System.Security.Cryptography.RNGCryptoServiceProvider class and the System.Random class