Computer Random Number Generation

Source: Internet
Author: User
Tags random seed

Generation of random numbers in computers

We may have discussed how random numbers are generated in computers many times. In this article, I will further discuss this issue and explain my understanding of this issue.

The first thing to declare is that the computer will not generate an absolute random number, and the computer can only generate a "pseudo random number ". In fact, an absolute random number is just an ideal random number. Even if the computer develops, it will not generate an absolute random number. A computer can only generate relative random numbers, that is, pseudo random numbers.

Pseudo-Random numbers are not pseudo-random numbers. Here, the "pseudo" is a regular meaning, that is, the pseudo-random numbers produced by computers are both random and regular. How can this problem be understood? The generated pseudo-random numbers sometimes follow certain rules, and sometimes do not follow any rules. Some pseudo-random numbers follow certain rules, while others do not follow any rules. For example, "There are no two leaves in the same shape in the world." This is just the characteristic of things, that is, randomness. However, the leaves of every tree have an approximate shape. This is the commonality of things, regularity. From this perspective, you will probably accept the fact that a computer can only generate pseudo-random numbers instead of absolute random numbers.

How is a random number produced in a computer? Some may say that random numbers are produced by random seeds. Yes, random seeds are used to generate a random number. In a computer, such a "Random Seed" is an unsigned integer number. Where can random seeds be obtained?

The following shows a C program:

// Rand01.c
# Include <dos. h>

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) describes the process of generating random numbers completely:
First, the main program calls the random_start () method. I am very interested in this sentence in the random_start () method:

Movedata (0x0040, 0x006c, fp_seg (temp), fp_off (temp), 4 );

This function is used to move memory data. fp_seg (far pointer to segment) Is the function to take the address of the temp array segment, and fp_off (far pointer to offset) Is the function to take the relative address of the temp array, the role of the movedata function is to put the dual characters in the 0040: 006ch storage unit into the two storage units declared by the array temp. In this way, a 16-bit number at 0040: 006ch can be sent to rand_seed through the temp array.

Random is used to calculate the random number based on the random seed rand_seed value. Here is the sentence:

Rand_seed = (rand_seed * 123 + 59) % 65536;

The random number calculation method is used to calculate random numbers. The random number calculation method is different in different computers, even in different operating systems installed on the same computer. I have tried it in Linux and Windows respectively. The random numbers generated by the same Random Seed in these two operating systems are different, which indicates that their calculation methods are different.

Now we know where the Random Seed is obtained and how the random number is calculated by the random seed. So why should random seeds be retrieved at 0040: CH in the memory? 0040: What is stored in ch?

Those who have studied computer composition principles and interface technology may remember that Intel 8253 Timer/counter will be used when preparing the rom bios clock to interrupt the service program, it communicates with the intel 8259 interrupt chip to enable the interrupt service program to run. The 18.2 interrupt produced by the motherboard per second is produced by the processor controlling the interrupt Chip Based on the timer/counter value. On our computer's motherboard, there will be such a Timer/counter used to calculate the current system time. Every time a clock signal period passes, the counter will be added to one, and where will the value of this counter be stored? That's right. It's at 0040: ch of memory. In fact, this memory space is defined as follows:

Timer_low DW? ; Address: 0040: 006ch
Timer_high DW? ; 0040: 006eh
Timer_oft DB? ; Address: 0040: 0070 H

In the clock interruption service program, whenever timer_low turns to full, the counter will also turn to full, and the value of the counter will return to zero, that is, the 16-bit binary at timer_low will return to zero, while timer_high will add one. In rand01.c

Movedata (0x0040, 0x006c, fp_seg (temp), fp_off (temp), 4 );

The 16-bit binary numbers timer_low and timer_high are put into the temp array and sent to rand_seed to obtain the Random Seed ".

Now, it can be determined that the random seed comes from the system clock. Specifically, it is the timer/counter value from the computer motherboard in the memory. In this way, we will summarize the previous analysis and discuss the application of these conclusions in the program:

1. the random number is calculated by the random seed based on a certain calculation method. Therefore, as long as the calculation method is certain and the random seed is certain, the random number generated will not change.

See the following C ++ program:

// Rand02.cpp
# Include <iostream>
# Include <ctime>
Using namespace std;

Int main ()
{
Unsigned int seed = 5;
Srand (seed );
Unsigned int r = rand ();
Cout <r <endl;
}

In the same platform environment, after an exe is compiled and generated, the random numbers displayed each time it is run are the same. This is because in the same compiling platform environment, the random number generated by random seeds is calculated in the same way. In addition, the random number is the same.

2. As long as the user or a third party does not set a random seed, the random seed is from the system clock (that is, the timer/counter value) by default)

See the following C ++ program:

// Rand03.cpp
# Include <iostream>
# Include <ctime>
Using namespace std;

Int main ()
{
Srand (unsigned) Time (null ));
Unsigned int r = rand ();
Cout <r <Endl;
Return 0;
}

If the user and other programs do not set a random seed, the system timer/counter value is used as the Random Seed. Therefore, in the same platform environment, compile and generate an EXE and run it each time, the displayed random number is a pseudo-random number, that is, the displayed result is different each time.

3. Suggestion: If you want to generate a random number sequence in a program, you must set a random seed at most once before the random number is generated.

See the following C ++ program used to generate a random string:

// Rand04.cpp
# Include <iostream>
# Include <time. h>
Using namespace STD;
Int main ()
{
Int rnum, M = 20;
Char * Ch = new char [m];
 
For (INT I = 0; I <m; I ++ ){
// As you can see, the random seed will be set multiple times in the program with the For Loop
Srand (unsigned) Time (null ));
Rnum = 1 + (INT) (RAND ()/(double) rand_max) * 36); // evaluate a random Value
Switch (rnum ){
Case 1: ch [I] = 'a ';
Break;
Case 2: ch [I] = 'B ';
Break;
Case 3: ch [I] = 'C ';
Break;
Case 4: ch [I] = 'D ';
Break;
Case 5: ch [I] = 'E ';
Break;
Case 6: ch [I] = 'F ';
Break;
Case 7: ch [I] = 'G ';
Break;
Case 8: ch [I] = 'H ';
Break;
Case 9: ch [I] = 'I ';
Break;
Case 10: ch [I] = 'J ';
Break;
Case 11: ch [I] = 'K ';
Break;
Case 12: ch [I] = 'l ';
Break;
Case 13: ch [I] = 'M ';
Break;
Case 14: ch [I] = 'n ';
Break;
Case 15: ch [I] = 'O ';
Break;
Case 16: ch [I] = 'P ';
Break;
Case 17: ch [I] = 'q ';
Break;
Case 18: ch [I] = 'R ';
Break;
Case 19: ch [I] ='s ';
Break;
Case 20: ch [I] = 'T ';
Break;
Case 21: ch [I] = 'U ';
Break;
Case 22: ch [I] = 'V ';
Break;
Case 23: ch [I] = 'W ';
Break;
Case 24: ch [I] = 'X ';
Break;
Case 25: ch [I] = 'y ';
Break;
Case 26: ch [I] = 'Z ';
Break;
Case 27: ch [I] = '0 ';
Break;
Case 28: ch [I] = '1 ';
Break;
Case 29: ch [I] = '2 ';
Break;
Case 30: ch [I] = '3 ';
Break;
Case 31: ch [I] = '4 ';
Break;
Case 32: ch [I] = '5 ';
Break;
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;
}

The running results show that each character of the random string is the same, that is, the generated character sequence is not random, so we need to set srand (unsigned) time (NULL )); remove from the for loop and put it in front of the for statement to generate a random character sequence, and the character sequence generated each time it is run will be different (haha, it may also be the same, however, the probability of such a situation is too small ).
If you change srand (unsigned) time (NULL); To srand (2), although the character sequences generated during a single run are random, however, the random string generated during each running is the same. Remove the srand sentence from the program.

In addition, you may encounter this situation. When using the timer control to program, you will find that a group of random numbers generated at the same time interval will appear regular, the random numbers generated by the user's command events appear random. Why? Based on the above analysis, you can quickly come up with the answer. This is because timer controls the time interval precisely by the computer clock Counter. The time interval is the same, and the difference between the values before and after the counter is the same. In this way, the clock value is linear, the Random Seed is linear, and the random number generated is also regular. The random numbers generated by user key events are indeed more random, because the events are caused by human keys, and people cannot guarantee strict key time intervals, even if they do it strictly, it cannot be completely accurate. As long as the time interval difference is one microsecond, the difference between the values before and after the stenographer is different, and the variation of random seeds loses the linear law, the generated random number is more irregular, so the random number is more random. This reminds me of various Lottery programs for the gala. If a lucky audience is generated by pressing a button, the random principle will be well implemented and the result will be more fair.

Finally, I will summarize two key points:
1. The computer's pseudo-random number is calculated by the random seed based on a certain calculation method. Therefore, as long as the calculation method is certain and the random seed is certain, the random number generated is fixed.
2. As long as the user or third party does not set a random seed, the random seed comes from the system clock by default.

(End)

Related Article

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.