Comparison Experiment of the system. Random class repeatedly generating random numbers in the ultimate Computing Environment

Source: Internet
Author: User

Do not reprint original images

By byoy (conmajia@gmail.com)

23. May 2012

Reserve all rights


Note: This article is for your reference only.


True random and pseudo random

Random Number is a very important tool in computer programming. Using it, we can accomplish many, many, and many things must also be done with random numbers. Therefore, random numbers are not random enough, which is critical for us. In software programming, for example, C # (similar to any other language), we use the system. Random class to obtain random numbers. Because it is a random number obtained using a specific algorithm, in essence, random numbers generated by random (or even generated by any software) are not real random numbers, it is only a pseudo-random number with a certain degree of random. This is the interpretation of pseudo-random numbers on Wikipedia.

Pseudo-Random numbers, or pseudo-random numbers, are calculated using a deterministic algorithm and appear to be random numbers. Therefore, pseudo-random numbers are not actually random. When calculating pseudo-random numbers, if the start value is not changed, the number order of pseudo-random numbers will not change. The randomness of a pseudo-random number can be measured by its statistical characteristics. Its main characteristic is the possibility of each number and its relationship with other numbers in the number order. The advantage of a pseudo-random number is that it is relatively simple to calculate, and it is difficult to calculate its algorithm using only a few values. Generally, people use a false random number, such as the time on the computer as the start value for calculating the pseudo-random number.

Almost all real random number generators (theoretically, real random numbers are only produced by physical phenomena in nature, that is, hardware) have their own patents, it is not accessible to ordinary people. Lists some companies' true random number generator patents.

By byoy (conmajia@gmail.com)

23. May 2012

Reserve all rights

Implementation principle of system. Random

The system. Random class we use is implemented based on the Donald E. knuth random number generator algorithm. from a practical perspective, the random degree is sufficient. In addition, random can use seeds of the int type for randomization, so we do not feel any problems during daily use.

Test

In fact, today I don't want to study real random numbers, but when using them, I think of a situation: if the computer is developing rapidly, and the computer can only generate pseudo random numbers, so our current general application.

Random rand; while (true) {// use the seed to initialize the random number Rand = new random (SEED); int value = Rand. Next (); // (time-consuming operation )}

In ultra-high-speed computing in the future, the "time-consuming operations" may be a little slow in the future and can be ignored in the future. In this case, the above statement turns into a pure loop to generate a random number. In this case, can the value generated by random be "random to a certain extent? Let's make an experiment.

Lab Conditions

CPU: I3 380 m 2.56 GHz 2-Core 4 thread

Memory: ddr3 1066 2 GB X2

Hard Disk: Toshiba mk3265gsx (320 GB) @ 5400 rpm

Runtime:. NET Framework Version: v2.0.50727 (C #2.0)

Development: Visual Studio 2005 team suite

OS: Windows XP SP3

Lab Methods

Simulate the most rigorous environment. Use code similar to the following to compare multiple random generation methods and corresponding Random Number Generation results by changing different Rand generation methods and count numbers.

// Sample code, unavailable for (INT I = 0; I <count; I ++) {Rand. Next ();}

By byoy (conmajia@gmail.com)

23. May 2012

Reserve all rights

Lab Cases

This experiment tests the following eight common random number generation methods:

1. default constructor

Use random Rand = new random (). This is the most simple generation method, which can be generated at a time without repetition. This is also the method recommended by msdn.

2. default constructor (repeated creation)

The default constructor is still used, but the random variable is re-created in each loop, that is, the Rand = new random () variable is not stopped, the same below.

3. Use constants as seeds

Use Rand = new random (10 ). Use constants as seeds to create Rand variables. Single time, similar to case #1.

4. Use constants as seeds (repeated creation)

Similar to case #2.

5. Use the time scale as the seed (repeated creation)

This is a common usage. Each time the current system time is used as the seed to create the Rand variable. Use datetime. Now. ticks (the value of this attribute indicates the number of intervals between January 1, 0001 milliseconds that have elapsed since midnight, January 1, 100 ).

Note that this method can only be created repeatedly, otherwise it will become case #3.

6. Use a random number as the seed (repeated creation)

This is a "seemingly random" method that uses another random variable and uses next () as the seed to create a new rand variable for each loop. Because I have never used the specific results, I need to wait for the experiment to read the data.

Note that this method can only be created repeatedly, otherwise it will become case #3.

7. trigonometric seed (repeated creation)

Use math. Sin () trigonometric function as the seed to create a new rand variable. (Lab only)

8. Complex Seeds

This is a method with high seed randomness and is commonly used by many experts. I have excerpted the method described in xiahouwen in this post. The GUID hash value, the current time ticks, and the counter are used to calculate the seed and generate the Rand variable. The algorithm is as follows.

Private Static int randomcount = 0; Private Static string createrandomtext () {randomcount ++; guid = guid. newguid (); int key1 = guid. gethashcode (); int key2 = unchecked (INT) datetime. now. ticks); int seed = unchecked (key1 * key2 * randomcount); random Rand = new random (SEED); int n = Rand. next (100000,999 999); // (Business Code, omitted )}

Experiment Principle

The experiment principle is relatively simple. It only counts the distribution of multiple random numbers generated within the generation range. Use Arrays for statistics, and then draw a statistical histogram in picturebox. At the same time, the program will count the maximum number of repeated occurrences and the number of random number hops to test the changes in the random generation result. The more jump variables, the more numbers generated, and the less numbers generated. The ideal situation is that the number of hops equals the number of builds, that is, each generated value is different from the previous one.

The implementation and code of the experiment program are not explained here, and it has nothing to do with this article.

Sample description

Below are two test examples marked as A and B respectively.

Sample

Sample B

This is the sample result of generating 100 random numbers. The range of the generated random number is from left to right along the X axis, and the width range of the entire sample image. Each random number r generated by random is + 1 on the histogram at x = R. Therefore, the height of each histogram line represents the number of occurrences of the coordinate values of the line. The text description in the figure indicates:

  • Maximum repetition: the maximum number of occurrences of the same value. For example, if 100 random numbers (, 2...) are generated, where 1 appears 60 times, the maximum repetition is 60.
  • Hop: When a random number is different from the previous one, the hop variable + 1
  • Time used to generate all (100) random numbers. The time does not include the display time.

Note that the height of the histogram is adaptive. Therefore, when comparing two different histograms, you must refer to the "maximum repetition" marked on each graph 」.

How to read sample images

See sample A and sample B in the previous section. The sample graph records the number of repetitions of random numbers generated by random. Therefore, the more "not repeated" the sample graph indicates the higher "randomness" of the test case. As shown in the figure, the larger the histogram lines, the better. The smaller the "maximum repetition" value, the better the jump.

By byoy (conmajia@gmail.com)

23. May 2012

Reserve all rights

Lab results

1. 100 generation

2. 1,000 generation

3. 10,000 generation

4. 100,000 generation

5. 1,000,000 generation

By byoy (conmajia@gmail.com)

23. May 2012

Reserve all rights

Result Analysis

From the experiment results, we often see a difference 」. Next we will analyze them one by one.

#1:

By default, the random variable created by the constructor in a single time faithfully completes the random number output distribution near "white noise.

#2:

Poor randomness. It also shows a problem, that is, the output of the random created by the same seed has extremely high similarity. This is related to the pseudo-random sequence. For more information, see #4.

#3:

Using constants to complete the initialization of the random, similar to #1, achieves a considerable degree of random output.

#4:

Each time you use the same seed to create the random, and take the first random number generated by the random, the obtained values are exactly the same. This fully demonstrates the pseudo-random sequence generated by the computer. If the seeds are the same and the sequence index is the same, the obtained values are (almost) the same. It proves that the random number obtained by random is not a real random number.

#5:

For high-intensity generation, #5 with time as the seed has the "random rise" problem. A careful analysis takes 1 million duplicates as an example. The total consumption time is 3757.835 milliseconds, and the average consumption is 3757 nanoseconds. The minimum time scale of datetime. ticks is 100 nanoseconds. It is reasonable to say that the seed cannot be changed too late. But in fact, when the seeds are dumped every time the random is created, we will find that every 20-20 ~ After 50 cycles, datetime. Now. ticks changes. This causes the output value range to be reduced by 20 ~ 50 times.

#6:

The random number generated in the form of #1 is used as the seed of the new random. The Randomness is guaranteed, and the white noise distribution can naturally be achieved.

#7:

Use math. Sin () as the seed. Since math. Sin () obtains-1 ~ So multiply it by a constant to enlarge. The problem caused by amplification is the shrinking of the value range, and the periodicity of sin (), which leads to the fixed seed value.

#8:

With high hopes and complicated computing, the very professional #8 has finally met the expectations and won the same noise as #1 and #3. The #6 and #8 Use Cases calculated repeatedly consume many resources. The actual results are the same as those of #1 and #3, because "White Noise cannot be white again 」. #5, #7 is worse, so in such a large amount of high strength, the simplest and most effective way is to use the single default constructor shown in #1 to create the random variable.

The above is a simple analysis made by myself through the experiment results. In view of my limited brain capacity, the conclusions are for reference only. If there are any errors or errors in this test, please kindly advise.

By byoy (conmajia@gmail.com)

23. May 2012

Reserve all rights

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.