Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - What is a pseudo-random number?
What is a pseudo-random number?
Category: Computer/Network

Analysis:

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

First of all, it needs to be declared that computers will not generate absolute random numbers, but only "pseudo-random numbers". In fact, an absolute random number is just an ideal random number. Even if the computer develops, it will not produce a series of absolutely random numbers. Computers can only generate relative random numbers, that is, pseudo-random numbers.

Pseudo-random numbers are not pseudo-random numbers. "Pseudo" here means regular, that is, computer-generated pseudo-random numbers are both random and regular. How to understand it? The generated pseudo-random number sometimes obeys certain laws, and sometimes does not obey any laws; Some pseudo-random numbers obey certain laws; The other part doesn't obey any laws. For example, "there are no two leaves with the same shape in the world", which points to the characteristics of things, that is, randomness, but the leaves of every tree have similar shapes, which is the * * * nature of things, that is, regularity. From this perspective, you will probably accept the fact that computers can only generate pseudo-random numbers, but not absolute random numbers.

So how are random numbers generated in computers? Some people may say that random numbers are generated by "random seeds". Yes, random seeds are numbers used to generate random numbers. In a computer, such a "random seed" is an unsigned integer. So where do random seeds come from?

Let's look at such a C program:

rand0 1.c

# Including

Static unsigned int RAND _ SEED

Unsigned int random(void)

{

RAND _ SEED =(RAND _ SEED * 123+59)% 65536;

return(RAND _ SEED);

}

Void random_start (invalid)

{

int temp[2];

movedata(0x0040,0x006c,FP_SEG(temp),FP_OFF(temp),4);

RAND _ SEED = temp[0];

}

Master ()

{

Unsigned int i, n;

random_start()。

for(I = 0; I< 10; i++)

printf("%u\t ",random());

printf(" \ n ");

}

This program (rand0 1.c) comprehensively expounds the process of generating random numbers:

First, the main program calls the random_start () method. I am 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, where FP_SEG (the far pointer to the segment) is the function of obtaining the segment address of the temp array, FP_OFF (the far pointer to the offset) is the function of obtaining the relative address of the temp array, and the function of movedata is to put the double words located in the 0040:006CH memory cell into the two declared memory cells of the temp array. In this way, the 16 digits of 0040:006CH can be sent to RAND_SEED through the temp array.

Random is used to calculate random numbers according to the value of random seed RAND_SEED, where this sentence:

RAND _ SEED =(RAND _ SEED * 123+59)% 65536;

Is a method for calculating random numbers. The calculation method of random number is different in different computers, even in different operating systems installed in the same computer. I tried it under linux and windows respectively, and the random numbers generated by the same random seed in these two operating systems are different, which shows that their calculation methods are different.

Now, we know where random seeds are obtained and how random seeds calculate random numbers. So, why do you want to get random seeds in memory at 0040:006CH? 0040:006CH What does CH store?

Those who have studied the course "Computer Organization Principle and Interface Technology" may remember that Intel 8253 timer/counter was used when writing the ROM BIOS clock interrupt service program. Its communication with the Intel 8259 interrupt chip enables the interrupt service program to run, and the 18.2 interrupt generated by the motherboard is generated by the processor controlling the interrupt chip according to the timer/counter value. There will be such a timer/counter on our computer motherboard to calculate the current system time. Every clock signal cycle will increase the counter by 1. Where is the value of this counter stored? Yes, it's in memory at 0040:006CH. In fact, this memory space is defined as follows:

TIMER_LOW DW? ; The address is 0040:006CH.

TIMER_HIGH DW? ; The address is 0040:006EH.

TIMER_OFT DB? ; The address is 0040:0070H.

In the clock interrupt service program, whenever TIMER_LOW becomes full, at this time, the counter will also become full, and the value of the counter will return to zero, that is, the 16 bit at TIMER_LOW will return to zero, and TIMER_HIGH will add 1. At rand0 1.c

movedata(0x0040,0x006c,FP_SEG(temp),FP_OFF(temp),4);

Just put two binary numbers of 16 bits, TIMER_LOW and TIMER_HIGH, into the temp array and send them to RAND_SEED, so as to get a "random seed".

What is certain now is that the random seed comes from the system clock, specifically, from the memory value of the timer/counter on the computer motherboard. In this way, let's summarize the previous analysis and discuss the application of these conclusions in the program:

1. Random number is a numerical value calculated by random seed according to a certain calculation method. Therefore, as long as the calculation method is certain and the random seed is certain, the generated random number will not change.

Look at the following C++ program:

rand02.cpp

# Including

# Including

Use namespace std

int main()

{

Unsigned intseed = 5;

Srand (seed);

Unsigned int r = rand ();

Cout & lt Editor's Note: There may be code gaps.

}

In the same platform environment, after compiling and generating exe, the random number displayed is the same every time it is run. This is because in the same compilation platform environment, the calculation method of generating random numbers by random seeds is the same, and the random seeds are the same, so the generated random numbers are the same.

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

Look at the following C++ program:

rand03.cpp

# Including

# Including

Use namespace std

int main()

{

Srand ((unsigned) time (empty));

Unsigned int r = rand ();

Cout & lt returned 0;

}

Here, users and other programs do not set random seeds, so they use the value of the system timer/counter as random seeds. Therefore, in the same platform environment, after compiling and generating exe, the random number displayed every time will be pseudo-random, that is, the result will be different every time.

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

Look at the following C++ program used to generate random strings:

rand04.cpp

# Including

# Including

Use namespace std

int main()

{

int rNum,m = 20

char * ch = new char[m];

for(int I = 0; As you can see, the random seed will be set many times in the program with the for loop.

Srand ((unsigned) time (empty));

rNum = 1+(int)((RAND()/(double)RAND _ MAX)* 36); Found 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;

Situation 6: ch [I] =' f';

Break;

Situation 7: ch [I] =' g';

Break;

Case 8: ch [I] =' h';

Break;

Situation 9: ch [I] =' I';

Break;

case 10:ch[I]= ' j ';

Break;

case 1 1:ch[I]= ' k ';

Break;

Case12: ch [I] =' l';

Break;

Case13: ch [I] =' m';

Break;

Case14: ch [I] =' n';

Break;

Case15: ch [I] =' o';

Break;

Case16: ch [I] =' p';

Break;

case 17:ch[I]= ' q ';

Break;

Case18: ch [I] =' r';

Break;

Case19: ch [I] =' s';

Break;

Case 20: ch [I] =' t';

Break;

case 2 1: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;

Scenario 27: ch [I] =' 0';

Break;

Case 28: ch [I] ='1';

Break;

Case 29: ch [I] =' 2';

Break;

Case 30: ch [I] =' 3';

Break;

case 3 1: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 < lt} for loop ends.

Cout & lt returned 0;

}

Moreover, every character of the random string displayed by the running results is the same, which means that the generated character sequence is not random, so we need to put SRAND ((unsigned) time (null)); Move it out of the for loop and put it in front of the for statement, so that a random character sequence can be generated, and the generated character sequence will be different every time (hehe, it may be the same, but the probability is too small).

If you put srand ((unsigned) time(NULL));); Change it to SRAND (2); In this way, although the character sequence generated by one run is random, the random character sequence string generated by each run is the same. The same is true of deleting the sentence srand from the program.

In addition, you may encounter this situation. When programming with timer control, you will find that a set of random numbers generated at the same time interval will appear regular, while a set of random numbers generated by mand events pressed by users will appear random. Why? According to our analysis above, you can get the answer quickly. This is because timer is a control whose time interval is precisely controlled by the computer clock counter. The time interval is the same, and the value difference before and after the counter is the same, so the clock value is linear, so the random seed is linear and the generated random number is regular. The random number generated by the user keystroke event is indeed more random, because the event is caused by the user keystroke, and people can't guarantee the strict keystroke interval, even if it is strictly done, it can't be completely accurate. As long as the time interval differs by one microsecond, the values before and after the counter are different, and the change of random seeds loses its linear law, so the generated random numbers are even more irregular, so a group of random numbers are even more random. This reminds me of the lottery procedures of all parties. If people use buttons to generate lucky viewers, the principle of randomness will be well realized and the result will be fairer.

Finally, I summarize two main points:

1. The pseudo-random number of the computer is a numerical value calculated by a random seed according to a certain calculation method. So as long as the calculation method is certain and the random seed is certain, the generated random number is fixed.

2. As long as the user or the third party does not set the random seed, the random seed comes from the system clock by default.