. NET generates random numbers using the System.Security.Cryptography.RNGCryptoServiceProvider class and the System.Random class

Source: Internet
Author: User

. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.