I'm trying to make the computer genearate a random number while it loops only 4 times; however, when it loops the console only outputs the same number, instead of different numbers. For instance,
Computer Generator: 10
Computer Generator: 10.....so on
{
//Declare the variables for which the user will guess the number
double guessuser;
int counter=0;
//Tell use what this program will do
cout<<"Computer will give you a random number. Guess which number: ";
//Computer gives a random number
while (counter<=4)
{
srand(time(0));
cout<<"\n\n\t\tComputer Generator: " << rand()%100;
counter++;
}
cout<<"your out!";
_getch();
return 0;
}
You are seeding the random number generator with the same number over and over. Because the while loop is so fast srand(time(0)) returns the same time and seeds the generator with the same number giving you the same number over and over.
Do:
1 2 3 4 5
srand(time(0)); // Seed it once
while (counter<=4){
cout<<"\n\n\t\tComputer Generator: " << rand()%100;
counter++;
}