Random number generator help

I'm having several problems with this code.

1. For some reason, for example, when I enter 5 as the amount of times I want to generate random numbers, it generates 6 times.

2. I need to be able to say what the maximum number generated was at the very end but I'm not sure how.

3. I need to have all the math done to 2 decimal places but it doesn't seem to be working.

Any and all help will be greatly appreciated!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
  #include <iostream>
#include <cstdlib>
#include <ctime>
#include <iomanip>

using namespace std;
int main() 

{

int numbers = 0;
int i,x,z,average,total = 0;
cout << setprecision(3);

cout << "Enter how many times you want to generate random numbers: ";
cin >> numbers;
cout << "\nThis number is also used to cover the range of random integers." << endl;
cout << "\nEnter a starting minimum number: " ;
cin >> x;

srand(time(0));

for (i = 1 ; i<numbers; i++)
	{
	z = rand () % numbers + (x+1);
	cout << "Random " << (i+1) << ": " << z << endl;
	total += z;		
	}

average = (total/i+1);

cout << "\nThe average is " << total << endl;
cout << "\nSum is " << total << endl;
cout << "\nThe maximum number is: ";
return 0;

}
I figured out the problem regarding number 1. Just help with the second and third question would be great!
2. Keep track of the largest random number so far as they're generated. In the for loop you can check if the newly generated random number is greater than the largest one you've seen so far.

3. You've defined the variable for average as an integer. If you want decimal points, it would need to be a double (or float). I'd also make total a double and initialize to 0.0 so you don't have integer division at line 30. Try using fixed in addition to setprecision(2).

See examples of fixed here: http://www.cplusplus.com/reference/ios/fixed/

(Line 32 - I think you mean to print out average, not total?)
I appreciate the reply!

So thanks to you I've solved number 2 but I can't figure out for the life of me how to find the greatest value produced by the RNG. Do I need an if statement?

Here's the code so far

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <iomanip>

using namespace std;
int main() 

{

int numbers = 0;
int i,x,z = 0;
double average, total = 0.0;
cout << setprecision(3);

cout << "Enter how many times you want to generate random numbers: ";
cin >> numbers;
cout << "\nThis number is also used to cover the range of random integers." << endl;
cout << "\nEnter a starting minimum number: " ;
cin >> x;

srand(time(0));

for (i = 0 ; i<numbers; i++)
	{
	z = rand () % numbers + (x+1);
	cout << "Random " << (i+1) << ": " << z << endl;
	total += z;	
	}

average = (total/i+1);

cout << "\nThe average is " << average << endl;
cout << "\nSum is " << total << endl;
cout << "\nThe maximum number is: ";
return 0;

}
Yes - you can use an if statement in the for loop to see if the newly generated random number is greater than the current max you have stored - if yes, then updated the max value stored.

Whatever variable you create to store the max value would be initialized so the first time through the for loop, you're comparing the random number to a valid value.
Alright I got it to work. Thanks so much for your help!
Topic archived. No new replies allowed.