srand+rand() combination

#include <iostream>
#include <cstdlib> /* include to allow rand() to be used */
#include<ctime>/*just used in function: time(NULL)*/
using namespace std;
int main()
{
 int x; /* variable to hold our random integer */
 srand(time(NULL));//system's time, in seconds,if not, the value will not change
 x = rand();
 cout << "x = " << x << endl;
 x = rand();
 cout << "x = " << x << endl;
 return 0;
} 

 

Guess you like

Origin www.cnblogs.com/defoliate/p/12242879.html