Rand is used for random application requirements. Shuffling, drawing, lotteries, automatic betting lottery…

for(int i=0; i<10; i++) { printf("%d\n",rand()) }Copy the code

Drawback of the rand() function: Regular PCS generate pseudorandom numbers. Because it’s physically hard for a PC to create random phenomena. The sequence of random numbers is the same every time the program runs. This randomness means that each rand () result is different if the program runs continuously. Set a seed random number is generated according to certain rules, pass in a parameter, make it produce a different result. (If srand is not called, srand(1) is executed by default.) Random number sequence generated after srAND (11) and srAND (12)

Srand (11); // Make sure that each time the program starts, a different seed for(int I =0; i<10; i++) { printf("%d\n",rand()); }Copy the code

The current time is passed to SRand each time the program starts

srand(time(NULL));
Copy the code

Generally, the result returned by rand() is modulated so that it falls within a range. For example, a random number between 100 and 160 int r=rand()%60+100; Where, 60 is the interval length, which ranges from 100 to 160