0
random number generator with system timer
how can you implement it?
1 Réponse
+ 2
#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;
int main()
{
srand(time(0));
for(int i=0;i<10;i++)
{
cout<<1+(rand()%6)<<" ";
}
cout<<endl;
return 0;
}
This program generates 10 random numbers in the range 1 to 6.
rand() function is used to generate any random numbers, but generates same number for every run. So srand() function is used to change value of random number generated by rand().
time(0) returns system time in seconds.
By passing different values to srand() function in each run rand() function generates different numbers.