The standard library <cstdlib> (included in <iostream>) provides two functions to help generate pseudo-random numbers:
Function 1: int rand (void );
Returns a random integer between [seed, rand_max (0x7fff) starting from the seed specified in srand (SEED.
Function 2: void srand (unsigned seed );
The seed parameter is the seed of rand () and used to initialize the starting value of rand.
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.
Based on the first point above, we can conclude that:
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 ).
For example, we want to obtain 0 ~ Random integer between 6 (excluding 6 itself ):
Example 1: do not specify seed:
For (INT I = 0; I <10; I ++ ){
Ran_num = rand () % 6;
Cout <ran_num <"";
}
Output for each operation: 5 5 4 4 5 4 0 4 2
Example 2: Specify seed as the value 1:
Srand (1 );
For (INT I = 0; I <10; I ++ ){
Ran_num = rand () % 6;
Cout <ran_num <"";
}
Output for each operation: 5 5 4 4 5 4 0 4 2
The result is exactly the same as that of Example 1.
Example 3: Specify seed as the value 6:
Srand (6 );
For (INT I = 0; I <10; I ++ ){
Ran_num = rand () % 6;
Cout <ran_num <"";
}
Output after each operation: 4 1 5 1 4 3 4 2 2
The random value is also between [0, 6). The random value is different from srand (1), but the result of each running is the same.
Example 4: Specify the seed as the elapsed time of the current system (unit: seconds): time_t time (0 ):
# Include <ctime>
//...
Srand (unsigned) time (0 ));
For (INT I = 0; I <10; I ++ ){
Ran_num = rand () % 6;
Cout <ran_num <"";
}
Output at first run: 0 1 5 4 5 0 2 3 4 2
Second: 3 2 3 0 3 5 5 2 2 3
In short, each running result will be different, because every time the program is started is different (the interval must be greater than 1 second? See ).
About time_t time (0 ):
Time_t is defined as a long integer. It returns the time elapsed since 00:00:00, January 1, January 1, 1970, in seconds. For example, assume that the output is:
Cout <time (0 );
The value is about 1169174701, which is equal to 37 (year) multiplied by 365 (days) multiplied by 24 (hours) multiplied by 3600 (seconds) (months and days are not counted ).
In addition, about ran_num = rand () % 6,
It is necessary to evaluate the modulo between the return value of rand () and 6, so that the random number of the target falls between [). Otherwise, the return value of rand () may be huge.
A general formula is:
To obtain a random integer between [a, B), use (RAND () % (B-a) + a (the result value will contain a Excluding B ).
When a is 0, it is abbreviated as rand () % B.
Finally, the pseudo-random Floating Point Number:
Use rand ()/double (rand_max) to obtain 0 ~ The floating point between 1 (note that, unlike the integer formula, it is divided by, not the modulo), for example:
Double rand_numf = 0.0;
Srand (unsigned) time (0 ));
For (INT I = 0; I <10; I ++ ){
Ran_numf = rand ()/(double) (rand_max );
Cout <ran_numf <"";
}
Running result: 0.716636, 0.457725 ,... And so on ~ The floating point number between 1 and the result is different each time.
If you want to get a larger range of random floating point numbers, such as 1 ~ 10.
Rand ()/(double) (rand_max) to Rand ()/(double) (rand_max/10)
Running result: 7.19362, 6.45775 ,... And so on ~ The floating point number between 10 and the result is different each time.
As for, and so on.
Re: http://blog.chinaunix.net/u2/82702/showart.php? Id = 2066067