Summary of random number knowledge in C++

The use of random numbers in C++

     Computers do not generate absolutely random random numbers, computers can only generate "pseudo-random numbers". In fact, an absolutely random random number is just an ideal random number. No matter how the computer develops, it will not generate a string of absolutely random random numbers. Computers can only generate relative random numbers, that is, pseudo-random numbers, with certain laws

The concept of seeds

    Random seed (unsigned type) is a number used to generate random numbers. In a computer, such a "random seed" is an unsigned integer number. So where did you get the random seed? Most computers use time to calculate, and the calculation method of random number (for) is as follows:

   RAND_SEED=(RAND_SEED*123+59)%65536;

    == Random numbers are calculated by random seeds according to certain calculation methods. Therefore, as long as the calculation method is fixed and the random seed is fixed, the random number generated will not change. ==
    Under the same platform environment, after compiling and generating exe, every time you run it, the random number displayed is the same. This is because in the same compilation platform environment, the calculation method for generating random numbers from random seeds is the same, plus the random seeds are the same, so the random numbers generated are the same.


The random() function cannot be used in C++

    The random function is not an ANSI C standard and cannot be compiled and passed under compilers such as gcc and vc. But int random(num) can be used like this in C language, it returns a random number from 0 to num-1. It can be realized by using the rand function under C++ instead.
    The C++ standard function library provides a random number generator rand, which returns a pseudo-random integer uniformly distributed between 0 and RAND_MAX. RAND_MAX must be at least 32767. The rand() function does not accept parameters and uses 1 as the seed by default (ie the starting value). The random number generator always starts with the same seed, so the pseudo-random number sequence formed is also the same, losing the meaning of randomness. (But this is convenient for program debugging)


Solve the problem of generating the same random string every time

    Another function srand() in C++ can specify a different number (unsigned integer argument) as the seed. But if the seeds are the same, the pseudo-random number sequence is also the same. One way is to let the user enter the seed, but it is still not ideal. It is ideal to use a variable number, such as time, as the seed of the random number generator. The value of time is different every moment. So the seeds are different, so the random numbers generated are also different. For example the following:

#include <stdio.h> 
#include <iostream> 
#include <time.h> 
using namespace std; 
#define MAX 100 
int main(int argc, char* argv[]) 
{ 
    srand( (unsigned)time( NULL ) );//srand()函数产生一个以当前时间开始的随机种子.应该放在for等循环语句前面 不然要很长时间等待
    for (int i=0;i<10;i++) 
      cout<<rand()%MAX<<endl;//MAX为最大值,其随机域为0~MAX-1
   return 0; 
} 

Guess you like

Origin blog.csdn.net/u012397189/article/details/78528441