seems to be giving me the same number everytime..am i using it wrong?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
double getrand ()
{
double probability, r;
cout << "How likely is the virus to spread to a neighbor (0 = never, 1 = certain)? ";
cin >> probability;
while (probability > 1 || probability < 0)
{
cout << "please enter a number between 0 and 1 ";
cin >> probability;
}
srand (probability);
r = (double)rand () / (double) RAND_MAX;
return r;
}
srand seeds the RNG. In order to get random numbers, you need to provide a seed that is, itself, somewhat random. Typically this is done by using the current time:
srand( time(0) );
Since srand() takes a unsigned integer as its seed, and not a double, your 'probability' is being truncated to either 0 or 1 (usually 0). So your above srand is not very effective.
+ what Bazzy said. Only call srand once. Like when your program first starts up.