Random number generator

Hello, could someone help me understand how to take a loop on a random number and choose the highest number from the loop?
Thank you for any help!
Hi, maybe you want something like this: ?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <ctime>
#include <iostream>
using namespace std;

int main ()
{
	const int NumberOfTests = 100; //How much numbers we will generate
	const int MaximumSize = 1000; //maximum value of random value
	int Biggest = 0; //var to save maximum value
	int Number; //var to save actual number
	srand(time(0)); //initializing random number generator
	for (int a = 0; a < NumberOfTests; ++a) //main cycle
		if ( (Number = rand()%MaximumSize) > Biggest) //test & generating random value
			Biggest = Number;
	cout << "Biggest number was: " << Biggest << endl;
	system ("PAUSE");
	return 0;
}


it is generating random values, and then shows the highest one
Since you have it based on the time clock hand for your random number generator, and loops are often able to process quicker than a second... it will come up with the same number for each loop within the same second.

Try putting a user pause in the middle of your loop. After the Biggest number have it pause. This will give enough time for the second to switch and you'll have a more likely random number.
How do you put one in and what exactly is a user pause?
Thank you for the help!
Both of you Thank you very much!
Thanks, for pauses I'm using the sleep function, I don't know if there is some other way to do that (except empty loops, or NOP, etc.):-)
 
Sleep(1); //one milisecond should be enough 
scurnutt, can you explain more clearly why you need to wait a second between each call to rand()? Because I wrote this:

1
2
3
4
5
6
int main(){
	srand(time(0));
	std::cout << rand() << "\n";
	std::cout << rand() << "\n";
	std::cout << rand() << "\n";
}

... And it executes in less than a second, and all the numbers are different from one another. That seems kind of contrary to what you said.

I suppose if srand(time(0)) were called between each rand() function, then all the rand()s would return the same value if they were within the same second. But that's not the case here- srand() is called only once.
If the whole program is executed twice in the same second, it will produce the same output. There's no reason to add extra pauses.
Topic archived. No new replies allowed.