There is no real random number generator in the computer, but it can make the generated digital repetition rate very low, it seems like a real random number, to achieve this function ProgramIt is called a pseudo-random number generator.
There are many theories about how to generate random numbers. To discuss them in detail, we need a thick book. No matter what method is used to implement the random number generator, you must provide it with an initial value named "Seed ".. And the value should be random, or at least the value is pseudo-random.. The value of the "seed" is usually generated using the fast count register or shift register.
The following describes the usage of the random number generator provided in the C language. Currently, the C compiler provides an ANSI-based pseudo-random number generator function to generate random numbers. They are the rand () and srand () functions. The two functions work as follows:
1) First, provide a seed for srand (). It is of the unsigned int type and its value ranges from 0 ~ 65535;
2) then call Rand (), which returns a random number (between 0 and 32767) based on the seed value provided to srand)
3) call Rand () multiple times as needed to continuously obtain new random numbers;
4) at any time, srand () can be provided with a new seed to further "randomize" the output results of rand.
This process looks very simple. The problem is that if you provide the same seed value every time you call srand (), you will get the same random number sequence. In this case, we can see that there is no random number, each time the number is the same. For example, after srand () is called as the seed value of 17, the random number 94 is obtained when rand () is called for the first time. When rand () is called for the second and third times, 26602 and 30017 are obtained respectively. These numbers seem very random (although this is just a small set of data points, after you call srand () with 17 as the seed value again, in the first three calls to Rand (), the returned values are still 30017, in addition, the subsequent returned values are the remaining returned values in the first call to Rand. Therefore, a random number can be obtained only when srand () is given a random seed value.