. NET base "extended" random number

Source: Internet
Author: User

A random number is defined as: All the numbers produced have no relation.

1. The principle of random numbers:

Linear same congruential: number of n+1 = (nth number *29+37)% 1000

Write a random number class of your own:

class Myrand {    privateint  seed;      Public Myrand (int seed) {this. Seed = seed;}     Public int Next ()   {     intis aPNS;      = Next;      return next;  } }

2.Random class generates random numbers

The default parameterless constructor of the random class can be seeded based on the current system clock, making a series of algorithms to obtain pseudo-random numbers within the required range.

New Random (); int i = Rd. Next ();

This random number can achieve some low-demand targets, but if the random class takes the system clock seed close or even exactly the same in high concurrency, there is a good chance of repetition, where the loop is used to illustrate

 for  (int0; i++)    {new Random (); // no parameter is used to seed the system clock     Console.WriteLine (Rd. Next (). ToString ());}

This example outputs 10 identical "random numbers".

Highlight the problem: because random random number algorithm is fixed, so the number calculated according to the same seed must be the same. And with the speed of the modern computer, the cycle is almost instantaneous, the seed is consistent, so there will be 10 cycles to output the same random number of cases.

So

When generating multiple random numbers in the for loop, place the new random () outside the loop.

3. How do I generate a true random number?

The true random number generator with "/dev/random" can be used under Linux/unix, and its data master comes from hardware interrupt information.

The Windows:cryptgenrandom () function is based on the current process ID, the current thread ID, the system-initiated tickcount, the current time, the high-performance counter value returned by the QueryPerformanceCounter, the user name, the computer name, The value of the CPU counter and so on to calculate.

Of course. NET can also use the RNGCryptoServiceProvider class (under the System.Security.Cryptography namespace) to generate a true random number

Reference article: "Random number is deceptive,. Net, Java, C for me to testify" http://www.cnblogs.com/rupeng/p/3723018.html

. NET base "extended" random number

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.