It can be considered that rand () is viewed every time it is called:
1) If you have previously called srand (SEED) and specified a value for seed, it will automatically call
Srand (SEED) is used once to initialize its starting value.
2) If you have not called srand (SEED) before, it will automatically call srand (1) once.
1) If you want rand () to generate different values each time the program runs, you must change the value of seed in srand (SEED, this variable value must be different each time the program runs (for example, the time elapsed so far ).
2) Otherwise, if you specify a value for seed, the value generated by rand () will be the same each time the program runs, although the value will be [seed, rand_max (0x7fff )) between a random value.
3) If srand (SEED) is not called before calling rand (), the effect will be the same as calling rand (1) and then calling rand () (1 is also a set value ).
The stdlib. h header file contains a macro # define rand_max 0x7fff
Rand () generates a random number between 0 and 0x7ffff, that is, between 0 and 32767.
Rand ()/(rand_max + 1.0) is equal to a decimal point between 0 and 1, because the maximum rand () is 32767, and the minimum value is 0, dividing by 32768 is a decimal place between 0 and 1 (not equal to 1). Then multiplied by 10 is the number between 0 and 10 (not equal to 10 ). after adding 1, it becomes a formula for finding a random number between 1 and 10.
The srand function is the initialization function of the random number generator. prototype:
Void srand (unsigned seed );
It needs to provide a seed, such:
Srand (1 );
Use 1 directly to initialize the seed.
However, the system time is often used for initialization.
Returns the system time from 00:00:00 GMT, January 1, 1970.
The number of seconds that have lasted until now, and then the time_t data is converted to the (unsigned) type before being passed to the srand function, that is:
Srand (unsigned) Time (& T ));
There is also a frequent usage that does not need to define the time_t type T variable, namely:
Srand (unsigned) Time (null ));
Directly pass in a null pointer, Because t data is often not obtained through parameters in your program.
Srand (INT) getpid ());
Use the program id (getpid () as the initialization seed. In the same program, this seed is fixed.
Random output of ten integers ranging from 0
# Include <stdlib. h>
# Include <stdio. h>
# Include <time. h>
Void main (void)
Int I, K;
Srand (unsigned) Time (null ));
For (I = 0; I <10; I ++)
K = rand () % 100;
Printf ("k = % d \ n", k );
Where srand () is stored is not a problem. The problem is that the interval between the two running srand (unsigned) Time (null) is large enough to allow (unsigned) time (null) reflects different values so that the seeds are different.
Put srand () in Main. Every time you run the program, it takes at least a few seconds from the end of the program to the end of the re-running. At this time, the seed is different, therefore, the numbers randomly generated are different.
Put srand () in other functions, and the computer code runs very quickly. The number of calls to this function in one second may be several hundred or even hundreds of times. (unsigned) Time (null) it may be too late to generate different values. The seeds are the same, so the numbers generated randomly are the same.