use of the Random function rand () and Srand () in C + +
First, Rand ()
Name of function: Rand
Function: Random number generator
Usage: int rand (void);
Header file: stdlib.h
Function Description:
The internal implementation of RAND () is made with linear congruential, which is not a true random number, because its period is particularly long, so in a certain
Can be seen as random in the range.
RAND () returns the range of a random number between 0 and Rand_max. The range of Rand_max is at least between 32767 (int). Use
The unsigned int double byte is 65535, and four bytes is the integer range of 4294967295. 0~rand_max each number is selected
The odds are the same.
When the user does not set a random number seed, the system default random number seed is 1.
RAND () produces pseudo-random numbers that are the same each time they are executed, and to be different, initialize it with the function srand ().
program Example:
- #include <iostream>
- Using namespace std;
- #include <stdlib.h>
- #include <time.h>
- #define MIN 1//range generated by random numbers
- #define MAX 10
- int main ()
- {
- int i;
- Srand ((unsigned) time (0));
- cout<<"Ten random numbers from" <<MIN<<
- "to" <<MAX<<":/n" <<endl;
- For (i=0; i<10; i++) //Generate random numbers
- {
- Cout<<min + (int) MAX * rand ()/(Rand_max + 1) <<"/t";
- }
- cout<<endl;
- return 0;
- }
Second, Srand ()
Function Name: Srand
Function: Initialize random number generator
Usage: void srand (unsigned int seed);
Header file: stdlib.h
Function Description:
The Srand () is used to set the random number seed when rand () produces a random number.
The parameter seed must be an integer and can usually be used as seed with the return value of time (0) or null.
If each seed is set to the same value, rand () produces a random number that is the same every time.
program Example:
- #include <iostream>
- Using namespace std;
- #include <stdlib.h>
- #include <time.h>
- #define MIN 0//range generated by random numbers
- #define MAX 99
- int main ()
- {
- int i;
- Srand ((unsigned) time (NULL));
- cout<<"Ten random numbers from" <<MIN<<
- "to" <<MAX<<":/n" <<endl;
- For (i=0; i<10; i++) //Generate random numbers
- {
- Cout<<min + rand ()% (MAX + MIN-1) <<"/t";
- }
- cout<<endl;
- return 0;
- }
Iii. the relationship between Rand () and Srand ()
Rand () and Srand () are used together, where Srand () is used to initialize a random number seed, and rand () is used to generate a random number.
Because the random number seed is 1 by default, and the random number generated by the same random number is the same, it loses the meaning of randomness, so the random number seed is initialized with the function srand () to make the random number different each time it gets. The Srand () parameter, with the time function value (that is, the current time), since two calls to the rand () function are usually different, thus guaranteeing randomness.
Iv. general expression formulae for generating a certain range of random numbers
To obtain a random integer of [a, b], use (rand ()% (b-a)) + A (the result value contains a does not contain a).
To obtain a random integer [A, b], use (rand ()% (b-a+1)) + A (the resulting value contains a and a).
To obtain a random integer (A, b], use (rand ()% (b-a)) + A + 1 (the resulting value does not contain a containing a).
(Overall, general formula: A + rand ()% n; where a is the starting value and N is the range of integers)
To get a random integer between A and B, another representation: A + (int) b * rand ()/(Rand_max + 1).
To obtain a floating-point number between 0~1, you can use RAND ()/double (Rand_max).
V. Reasons for generating the same random number
The random number of a computer is generated by a pseudo-random number, which is a sequence of small m-polynomial, in which each small sequence has an initial value, that is, a random seed. (Note: The period of the small m polynomial sequence is 65535, that is, the period of the random number generated by each random seed is 65535, and when you get 65,535 random numbers, they repeat.) ）
We know that the rand () function can be used to generate random numbers, but this is not a random number in the true sense, a pseudo-random number, a series of numbers derived from a recursive formula based on a number (which we can call a seed), and when the series number is large, it conforms to the normal publication, This is equivalent to generating a random number, but this is not a true random number, and the value of this seed is fixed when the computer is booted properly, unless you destroy the system.
To get a different sequence of random numbers, you need to change the value of this seed. Method: Call Srand (Time (NULL)) once before starting to generate a random number (note: Srand () must be placed outside the loop or outside the loop call, otherwise the same random number is obtained).
Examples of programs:
- #include <iostream>
- Using namespace std;
- #include <stdlib.h>
- #include <time.h>
- int main ()
- {
- int i;
- Srand ((unsigned) time (NULL)); //Initialize random number seed
- For (i=0; i<10; i++) //Generate 10 random numbers
- {
- Cout<<rand () <<"/t";
- }
- cout<<endl;
- return 0;
- }
Each run gets a different random sequence:
1294 18562 14141 18165 11910 29784 11070 13225 131 24405
1774 25714 18734 16528 20825 17189 9848 8899 2503 5375
Reprint Link: http://lib.csdn.net/article/cplusplus/26833?knId=1175
Random functions rand () and Srand ()