C + + generates a random number using the rand (), Srand () function, which summarizes two function features and uses.
1. Rand ()
#include <iostream> #include <cstdlib>using namespace Std;int main () {int m;for (int i = 1; I <= 5; i++) {m = R and (); cout << "M =" << m << Endl;} return 0;}
2. Srand ()
#include <iostream> #include <cstdlib> #include <ctime>using namespace Std;int main () {int M;srand ( unsigned) time (NULL)), for (int i = 1; I <= 5; i++) {m = rand (); cout << "M =" << m << Endl;} return 0;}
3. Formula for generating a certain range of random numbers
Gets the random integer [A, b], using (rand ()% (b-a)) + A;
Gets a random integer [A, b], using (rand ()% (b-a+1)) + A;
Gets a random integer (A, b], using (rand ()% (b-a)) + A + 1;
To get the floating-point number between 0~1, you can use RAND ()/double (Rand_max)
[Reference article: If there is infringement, please inform, delete immediately]
1. http://www.cnblogs.com/afarmer/archive/2011/05/01/2033715.html
2. http://blog.csdn.net/peixuan197/article/details/48084843
C + + random number rand (), Srand ()