#include
#include
#include
int main()
{
/*
Declare variable to hold seconds on clock.
*/
time_t seconds;
/*
Get value from system clock and
place in seconds variable.
*/
time(&seconds);
/*
Convert seconds to a unsigned
integer.
*/
srand((unsigned int) seconds);
/*
Output random values.
*/
cout<< rand() << endl;
cout<< rand() << endl;
cout<< rand() << endl;
return 0;
}
2006-07-11 22:18:44
·
answer #1
·
answered by Joe_Young 6
·
0⤊
0⤋
rand() generates pseudo-random numbers. frequently, it takes the exceptional random volume and does a group of operations on it to calculate the subsequent random volume. So, in case you do not grant it a starting up volume, it is going to repeatedly start up on the same aspect. hence, it is going to repeatedly output the same numbers. to fix this, you grant it a starting up volume (seed). The seed is many times in ordinary words the time, simply by ordinary incontrovertible truth which will be distinct each and each and every time you run this technique. #contain ctime // elect to contain the time header int numIWant = 0; srand(time(0)); // Seed the generator. on the same time as (numIWant !=10) { numIWant = rand() % 10 + a million; cout << numIWant; } be wide wide awake a million: this retains to be pseudo-random because, in case you knew precisely what time your software ran, you ought to probably favor to comprehend precisely what numbers ought to favor to be generated. be wide wide awake 2: you in common words elect to call srand once.
2016-11-06 06:11:39
·
answer #2
·
answered by Anonymous
·
0⤊
0⤋