Random Number Generator HELP PLEASE

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

I want it to generate different numbers. :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
{
	//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++;
}
Last edited on
Yes, it looks you have no clear understanding on random number sequences and they seeds.

Here is the dedicated article at my website:
http://codeabbey.com/index/wiki/random-numbers

Perhaps this may help you to catch the idea...

and by the way here are two exercises about random sequences of two different kinds, if you want to practice this "magic":
http://codeabbey.com/index/task_view/neumanns-random-generator
http://codeabbey.com/index/task_view/linear-congruential-generator
Topic archived. No new replies allowed.