Thanks for sharing the platform-http: //bjbsair.com/2020-04-10/tech-info/53235.html
Foreword
This article mainly explains the random function of C language. Learning random function will start from this. Are you ready?
random number
Random numbers are mainly generated by the rand () function. The rand () function can randomly generate a positive integer. It will generate an unsigned integer in the range of 1 to 32767, which is the maximum value of a two-byte 16-bit integer. The range of random numbers generated by GNU C ++ is 2147483647. Each number in the range has the same probability to be selected each time rand is randomly called. ** The #inlcude <stdlib.h> header file needs to be added when using random functions. ** Such as the following case:
** Note: The answer to the result of each program run is the same, which is obviously not what we want. ** This is because the rand function is not a true random number generator, and srand () sets a random number seed for rand (). If you do not call srand () before the first call to rand (), then the system will automatically call srand () for you. Calling rand () with the same number as the seed will cause the same sequence of random numbers to be generated. If we want to make sure that what is produced is different every time, we need to refer to a function srand () that sets a random seed for rand.
Random function seed
The random function seed srand function is described in the library as follows:
//接口说明:
time() returns the time as the number of seconds since the Epoch,
1970-01-01 00:00:00 +0000 (UTC).
If tloc is non-NULL, the return value is also stored in the memory pointed to by tloc.
The meaning is: if the random result is different every time, you need to change the seed every time. The time function returns the description of the current time distance 197-01-01. Every time it runs different, it can be used as a seed, that is, everyone learn In the rand () function, the teacher asked everyone to write the reason for srand ((unsigned int) time (NULL)), so that the random number is bound to time, the time is changing, and the random number can naturally change.
In fact, the parameters of the srand function can be set arbitrarily. Pseudo-random means that the data obtained every time can be followed regularly. By default, the number is 10x7fff, which is (1 32767). When the setting parameter is srand (1000), the generated random number is 1000 ~ 0x7fff.
The following example:
Random function fixed range
The main idea is actually very simple, just do it by taking the balance