Generation of random numbers in the computer
Everyone may discuss how the random number is generated in the computer. In this article, I will discuss this problem, explain my understanding of this problem.
First, it is necessary to declare that the computer does not generate absolute random random numbers, and the computer can only generate "pseudo random". In fact, the absolute random random number is just an ideal random number, even if the computer develops, it does not generate a string of absolute random random random. Computers can only generate relative random numbers, ie pseudo-random numbers.
The number of pseudo-random is not a fake random number. The "pseudo" here is regular, that is, the number of pseudo-random generated by the computer is both random and regular. How to understand? The number of pseudo-random generated is sometimes abide by certain laws, sometimes not obeying any law; some of the number of pseudo randoms abide by certain laws; the other part does not comply with any law. For example, "There are no two shaped leaves in the world", which is the characteristics of things, namely random, but the leaves of each tree have approximate shapes, which is the commonality of things, regularity. From this perspective, you will probably accept this fact: the computer can only produce a pseudo-random number and cannot produce a random number of random.
So how is the number of random numbers in your computer? Some people may say that the random number is produced by "random seeds". That's right, the random seed is used to generate a number of random numbers. In the computer, such a "random seed" is an unsigned shape. So where is the random seed?
Look at such a C program:
//rand01.c#include
Static unsigned int RAND_SEED;
Unsigned int Random (void) {rand_seed = (Rand_seed * 123 59)% 65536; return (rand_seed);
Void random_start (void) {int Temp [2]; MoveData (0x0040, 0x006c, fp_seg (temp), fp_off (temp), 4); rand_seed = temp [0];
Main () {unsigned INT I, N; Random_Start (); for (i = 0; i <10; i ) Printf ("% U / T", Random ()); Printf ("/ n");}
This program (RAND01.C) completely elaborated the process of random number: First, the main program calls the random_start () method, this sentence in the Random_Start () method is very interested:
MoveData (0x0040, 0x006C, FP_SEG (TEMP), FP_OFF (TEMP), 4);
This function is used to move the memory data, where fp_seg (far point to segment) is a function of taking the TEMP array segment address, the fp_off (far point to offset) is a function of taking the TEMP array relative address, and the MOVEDATA function is based on 0040: The double word in the 006ch memory cell is placed in two storage units of the declaration of the array TEMP. This can be given to the RAND_SEED of a 16-bit number at 0040: 006CH through the TEMP array.
Random is used to calculate the random number according to the value of the random seed rand_seed, in which this sentence:
Rand_seed = (rand_seed * 123 59)% 65536;
It is used to calculate the random number, and the random number of calculations is different in different computers, even in different operating systems installed in the same computer. I tried it under Linux and Windows, the same random seeds were different in these two operating systems, which indicates that their calculation methods are different. Now, we understand where the random seed is obtained, and how the random number is calculated by random seeds. So why is the random seed to get at 0040: 006CH in memory? What is stored at 0040: 006CH?
People who have learned "Computer Composition Principles and Interface Technology" may remember to use the Intel 8253 timing / counter when preparing the ROM BIOS clock interrupt service program, and communication with Intel 8259 interrupt chip makes the interrupt service program operates The 18.2 interrupt generated by the motherboard is exactly the processor generated by the processor based on the timing / curler value. There will be such a timing / curler on the motherboard of our computer to calculate the current system time, every time a clock signal cycle will add the decisor, and where is the value of this deciple? That's right, just at 0040: 006CH of memory, in fact, this memory space is defined in this:
Timer_low dw?; Address 0040: 006ch Timer_high dw?; Address 0040: 006EH TIMER_OFT DB?; Address 0040: 0070H
In the clock interrupt service program, whenever Timer_low is full, at this time, the curler will also be filled, the value of the decisor is zero, that is, 16-bit binary from Timer_LOW, and Timer_High adds one. Rand01.c
MoveData (0x0040, 0x006C, FP_SEG (TEMP), FP_OFF (TEMP), 4);
It is putting Timer_low and Timer_High two 16-bit binary numbers into the TEMP array, and then sent to RAND_SEED, thereby obtaining "random seeds".
Now, it is determined that the random seed comes from the system clock, exactly, is the count value from the timing / counter on the computer motherboard. In this way, we summarize the previous analysis and discuss these conclusions in the program:
1. The random number is the value calculated by the random seed according to a certain calculation method. Therefore, as long as the calculation method must be sure, the random seed must not change.
Look at this C program:
//rand02.cpp#include
INT main () {unsigned int seed = 5; SRAND (SEED); unsigned int R = rand (); cout << r << endl;}
After the same platform environment, after compiling the EXE, each time it runs it, the random number of displayed is the same. This is because in the same compilation platform environment, all calculation methods of random seed generation random number are the same, while adding a random seed, the random number generated is the same.
2. As long as the user or third party does not set the random seed, then the random seed from the system clock (ie the value of the timbe / counter) by default.
Look at this C program:
//rand03.cpp#include
INT main () {srand (null); unsigned int R = rand (); cout << r << endl; return 0;} The user and other programs are not set to set random seeds, then use the system The value of the timing / counter is used as a random seed. Therefore, after compiling the EXE in the same platform environment, each time it runs, the random number of the display will be a pseudo-random number, that is, each running display will have different results.
3. Recommendation: If you want to generate a random number sequence in a program, you need to set up a random seed before generating random numbers.
Look at this C program used to generate a random string:
//rand04.cpp#include CASE 33: CH [I] = '6'; Break; Case 34: CH [I] = '7'; Break; Case 35: CH [I] = '8'; Break; Case 36: CH [i] = '9'; Break;} // end of switch cout << CH [i];} // end of for loop cout << endl; return 0;} and each character displayed by the random string is displayed. The same, that is, the generated character sequence is not random, so we need to put the SRAND TIME (NULL); from the FOR loop out in front of the FOR statement, this can generate a random character sequence, and each time Running the generated character sequence will be different (huh, it may also be the same, but this situation is too small). If you convert Srand (NULL)); change to SRAND (2); though although the character sequence generated in one run is random, the random character sequence string generated at time run is the same. This is the same as the SRAND. In addition, you may encounter this situation. When you use the Timer Control to prepare a program, a set of random numbers generated by the same time interval will appear regular, and a set of random numbers generated by the user button Command event It seems more random, why? Based on our analysis, you can quickly think about answers. This is because Timer is a control of the time interval by the computer, the time interval is the same. The difference in the value before and after the counter is the same, so the clock value is linear, so the random seed is linear. The random number of generated is also regular. The random number of user button events does have a randomness, because the event is caused by the person button, and people can't guarantee strict buttons time intervals, even strictly do it, it is impossible to completely do it, as long as the time interval is different One microseconds, the difference between the value before and after the decisor is different, and the change in the random seeds lost the linear law, and the generated random number is more unregistered, so this generated set of random numbers is more random. This reminds me of a variety of evening lottery programs. If you use people to create a lucky audience, then you will have a good starting principle, and the result will be more justified. Finally, I summarize two points: 1. The number of pseudo-random numbers of the computer is the value calculated by the random seed according to a certain calculation method. Therefore, as long as the calculation method must be determined, the random seed must be fixed, then the resulting random number is fixed. 2. As long as the user or third party does not set the random seed, the random seed is from the system clock by default. (Finish)