random numbers and getting their Sum, and Avg

what i'm trying to do with this program is have the randomly generated numbers add up to equal a sum, then in another line show the average between them all, then on another line say the highest number which was randomly generated. I tried looking up a solution on YT but nothing helpful came up in my searches. Anyone have an idea on how i can get the sum, average, and highest number? thanks

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
  #include <iostream>
#include <ctime> // time function
#include <cstdlib> // rand and srand functions
#include <cmath>
#include <math.h>

using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main() {
	// values
	int rngn;	
	int startnum;
	int round;
	
	
	srand(time(0));
	
	cout<<"How many times would you like the Random Number Generator to generate numbers: ";
	cin>> rngn;
	
	cout<<"    "<<endl;
	cout<<"This number will also be used as the range of random integers."<< endl;
	cout<<"    "<<endl;
	cout<<"Enter a starting number: ";
	cin>> startnum;
	
	for(round = 1; round <= rngn; round++){
		cout<<"Random number "<< round << ": " << rand() + 1 <<endl;
		
	}
	
	
	system("pause");
	return 0;
}
Last edited on
a little surgery ...
save the result of your random number into a variable (in the for loop).
then you can also have sum += result to get a total as you generate them.
after the loop, sum /= round for the average
and highest I will leave to you as an exercise once you have these down it should be obvious how to do it. (hint you can find it in the same for loop that generates them and finds the running sum..)
I'm sorry i'm very new but I don't get saving my random number into a variable?
like
int x;
x = rand();

you will want more than one int to store the numbers

so create an array of ints and assign them using a for loop

and yes you would do it like you mentioned

1
2
  // put this in for loop
  x[i] = rand() % 100; // random number from 0 to 99 

I tried adding it and now the program does nothing. did I miss something?

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
#include <iostream>
#include <ctime> // time function
#include <cstdlib> // rand and srand functions
#include <cmath>
#include <math.h>

using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main() {
	// values
	int input;	
	int startnum;
	int round;
	int i;
	int x[i];
	
	srand(time(0));	
	
	cout<<"How many times would you like the Random Number Generator to generate numbers: ";
	cin>> input;
	cout<<"    "<<endl;
	cout<<"This number will also be used as the range of random integers."<< endl;
	cout<<"    "<<endl;
	cout<<"Enter a starting number: ";
	cin>> startnum;
	
	for(round = 1; round <= input; round++){
		
		cout<<"Random number "<< round << ": " << rand() + 1 <<endl;	
		x[i] = rand() % 100;
		
		
	}
	
	
	system("pause");
	return 0;
}
closed account (E0p9LyTq)
What does startnum supposed to do? You don;t use it other than to input a value into it.

Is it supposed to be the maximum value your random numbers can have?

What is the minimum value?
I don't even know anymore i've been trying to do this since saturday lol.
i think its the range of random numbers
this is where I'm at now so i can get the desired amount of random numbers. but when the sum part comes up it always says 0 and then the average is going to the power and that's not right. i'm not trying to be a leech its just my prof isn't very helpful and the textbook doesn't talk about random number problems.

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
41
42
43
44
45
46
#include <iostream>
#include <ctime> // time function
#include <cstdlib> // rand and srand functions
#include <cmath>
#include <math.h>

using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main() {
	// values
	int gen_num, startnum, rand_num, i;
	double sum(0), average;
	int x[i];
	
	srand(time(0));	
	
	cout<<"How many times would you like the Random Number Generator to generate numbers: ";
	cin>> gen_num;
	cout<<"    "<<endl;
	cout<<"This number will also be used as the range of random integers."<< endl;
	cout<<"    "<<endl;
	cout<<"Enter a starting number: ";
	cin>> startnum;
	
	for(rand_num = 1; rand_num <= gen_num; rand_num++){
		cout<<"random number "<< rand_num << ": " << (rand() % 100) + 1 << endl;
		if(rand_num== gen_num){
			break;
			
		}else{
			continue;}
		x[i] = rand() % 100;	
		
		sum+= x[i];
		average = sum/gen_num;
	}
	cout<< "sum = " << sum << endl;
	cout<< "The average between all random numbers is " << average << endl;
	
	
	
	
	system("pause");
	return 0;
}
closed account (E0p9LyTq)
If I had to make a guess as to what you are trying to do I'd guess something like this:
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <iostream>
#include <ctime>
#include <cstdlib>

int main()
{
   srand(time(0));

   // discard the first random number generated
   rand();

   // create a typedef to ensure integer wrap around is not likely to occur
   using ULONG = unsigned long long;

   std::cout << "How many random numbers do you want to generate? ";
   ULONG num_rnd;
   std::cin >> num_rnd;

   std::cout << "\nWhat is the maximum value random number to generate? ";
   ULONG max;
   std::cin >> max;
   std::cout << '\n';

   ULONG count   = 0;
   ULONG sum     = 0;
   ULONG largest = 0;

   do
   {
      count++;

      ULONG num = rand() % max + 1;

      if (num > largest)
      {
         largest = num;
      }

      sum += num;

      std::cout << num << ' ';

      if (0 == count % 15)
      {
         std::cout << '\n';
      }
   } while (count < num_rnd);

   std::cout << "\n\nThe sum is: " << sum << '\n';

   std::cout << "The largest random number generated was: " << largest << '\n';

   std::cout << "The integer average is " << sum / num_rnd << '\n';
}
How many random numbers do you want to generate? 20

What is the maximum value random number to generate? 250

40 156 98 54 100 238 213 224 27 219 42 39 93 88 77
18 180 47 119 19

The sum is: 2091
The largest random number generated was: 238
The integer average is 104
but if I had to use a for() loop how would I do it?
@ colton96, one possible way would be to change from:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
	do
	{
		count++;

		ULONG num = rand() % max + 1;

		if (num > largest)
		{
			largest = num;
		}

		sum += num;

		std::cout << num << ' ';

		if (0 == count % 15)
		{
			std::cout << '\n';
		}
	} while (count < num_rnd);


To something like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
	for (count = 1; count < num_rnd; count++) // The variable count is declared and defined on line 24 as ULONG already.
	{
		ULONG num = rand() % max + 1;

		if (num > largest)
		{
			largest = num;
		}

		sum += num;

		std::cout << num << ' ';

		if (0 == count % 15)
		{
			std::cout << '\n';
		}
	}
thank you all for the help :) I really appreciate it, i'm slowly starting to understand how C++ works.
closed account (E0p9LyTq)
chicofeo's for loop has a subtle error, the number of generated random numbers will be one less than the user specified. It should be:

for (ULONG count = 1; count <= num_rnd; count++)
closed account (E0p9LyTq)
C++ has excellent means to generate random numbers in the <random> library, with a bit of extra setup work:

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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <iostream>
#include <random>
#include <chrono>

int main()
{
   // construct a trivial random generator engine from a time-based seed:
   unsigned seed = static_cast<unsigned> (std::chrono::system_clock::now().time_since_epoch().count());

   std::default_random_engine eng(seed);

   // create a typedef to ensure integer wrap around is not likely to occur
   using ULONG = long long;

   std::cout << "How many random numbers do you want to generate? ";
   ULONG num_rnd;
   std::cin >> num_rnd;

   std::cout << "\nWhat is the maximum value random number to generate? ";
   ULONG max;
   std::cin >> max;

   std::cout << '\n';

   // create an integer distribution:
   std::uniform_int_distribution<ULONG> dis(1, max);

   ULONG sum     = 0;
   ULONG largest = 0;

   for (ULONG count = 1; count <= num_rnd; count++)
   {

      ULONG num = dis(eng);

      if (num > largest)
      {
         largest = num;
      }

      sum += num;

      std::cout << num << ' ';

      if (0 == count % 15)
      {
         std::cout << '\n';
      }
   }

   std::cout << "\n\nThe sum is: " << sum << '\n';

   std::cout << "The largest random number generated was: " << largest << '\n';

   std::cout << "The integer average is " << sum / num_rnd << '\n';
}

A tutorial on creating random numbers (including why using rand() is not a good idea when creating C++ code):

https://www.learncpp.com/cpp-tutorial/59-random-number-generation/
@FurryGuy, thanks for the correction. 😊
Topic archived. No new replies allowed.