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
#include <iostream>
#include <ctime> // time function
#include <cstdlib> // rand and srand functions
#include <cmath>
#include <math.h>
usingnamespace 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;
}
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..)
#include <iostream>
#include <ctime> // time function
#include <cstdlib> // rand and srand functions
#include <cmath>
#include <math.h>
usingnamespace 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;
}
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.
#include <iostream>
#include <ctime> // time function
#include <cstdlib> // rand and srand functions
#include <cmath>
#include <math.h>
usingnamespace 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;
}
#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 = unsignedlonglong;
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
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';
}
}
#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 = longlong;
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):