473,396 Members | 2,129 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Adding set of generated random numbers

6
Hi everyone. First time here in bytes, but, I have a problem here dealing with adding a generated set of random numbers. Here is the code. And I recommend that you guys run it to see what I'm talking about.
Expand|Select|Wrap|Line Numbers
  1.  #include <iostream>
  2.  #include <iomanip>
  3.  #include <cstdlib>
  4.  #include <cmath>
  5.  
  6.  using namespace std;
  7.  //declarations
  8.  int seed, n, lower, upper, num;
  9.  double sum1, sum2, avg;
  10. bool repeat = true;
  11.  
  12.  //function prototypes
  13. void getParameters(int& seed, int& n, int& lower, int& upper);
  14. int getNum(int lower, int upper, int n, int seed);
  15. void update(int num, double& sum1, double& sum2);
  16. //void output(double avg, double std);
  17.  
  18.  int main (){
  19.      char input;
  20.      do {
  21.       getParameters(seed,n,lower,upper);
  22.       getNum(lower, upper, n, seed);
  23.       update(num,sum1,sum2);
  24.       //output(avg, std); 
  25.      cout<<endl;   
  26.      cout<<"Enter Y or y to repeat, N or n to exit. ";cin>>input;
  27.      }while(input == 'Y' || input == 'y');
  28.      if (input == 'N' || input == 'n') repeat = false;
  29.  system ("pause");
  30.  return 0;
  31.  }
  32.  
  33. /***************************************************************
  34. The function gets the seed, size, upper, and lower bounds used
  35. to generate the random numbers.
  36. ***************************************************************/
  37. void getParameters(int& seed, int& n, int& lower, int& upper){
  38.   cout<<"Seed for random number generator: ";cin>>seed;
  39.   cout<<"The size of the sequence: ";cin>>n;
  40.   cout<<"Lower and upper bound for the random number:";cin>>lower>>upper;
  41. }
  42.  
  43. /***************************************************************
  44. This function generates the random numbers based on the user input.
  45. ***************************************************************/
  46. int getNum(int lower, int upper, int n, int seed){
  47.     int i, a;
  48. cout<<"The numbers generated are: "<<endl; 
  49. srand(seed);   
  50. for (i = 1; i <= n; i++){
  51.     a=(rand()%(upper-lower)+lower);
  52. cout<<setw(4)<<a;
  53. }
  54. cout<<endl;
  55. return a;
  56. }
  57.  
  58. /***************************************************************
  59. This function should add up all the random numbers (sum1), and 
  60. square it (sum2).
  61. ***************************************************************/
  62. void update(int num, double& sum1, double& sum2){
  63.  
  64.      }
  65. /***************************************************************
  66. This function will output the average and standard deviation
  67. using the values from sum1 and sum2.
  68. ***************************************************************/
  69. //void output(double avg, double std){
  70. //     }
  71.  
Okay so here's the deal. If you run it, it's pretty straightforward. Program asks the user for the seed, upper and lower bounds, and size of the sequence to generate the random numbers. Thing is, as seen in my getNum function, I assigned the "randomizing" equation to a variable a. Now in the update function, I need to get the sum of those values (generated in getNum) to move on to the next phase of my coding (average and standard dev). My question is, is it possible to find the sum in getNum inside the update function? Am I even doing it the right way? Because it generated a set of values, because then these values are assigned to one variable, which I know is totally impossible, because one variable should only store one value. So I'm pretty much stuck. Also, most of the codes that I've seen while finding a solution is assigning the rand() to different variables, and then adding the values in those variables. Still don't get it? Okay, the codes that I've seen are, let's say:
a=rand(); equals some random value
b=rand(); equals some random value
c=rand(); equals some random value
sum = a+b+c;
Mine is:
(In getNum function)
a=rand(); equals multiple random values (because there's a for loop involved)
(In update function)
sum=?? (how to add these values??) <-- and finding the sum is in another function
Is my procedure kinda right? Or do I have to do the summation inside the getNum function and just "transfer" it to the update function? As an added catch, no arrays should be used. I know it's something simple, but for some reason I can't find the right operator or function or something for it. Anyways, here it is. Comments and suggestions are welcome. Thanks very much for helping. (Better get myself working again!!)
Oct 9 '11 #1
3 2753
Rabbit
12,516 Expert Mod 8TB
Use a separate variable to keep a running sum.
Oct 9 '11 #2
jp0424
6
yes, yes, yes, and thank you so much Rabbit. I was actually thinking on using the operator "+=" on this problem, but I was having doubts. I dug up some info about the running sum that you recommended and it used the operator "+=", so that pretty much solved my problem there. I'll post the complete working code, for future references to other users. That's it and thanks again.
Oct 9 '11 #3
jp0424
6
So here is the final code. Feel free to test this and salvage any codes that you wish to use in your codes.

Expand|Select|Wrap|Line Numbers
  1.  
  2.  #include <iostream>
  3.  #include <iomanip>
  4.  #include <cstdlib>
  5.  #include <cmath>
  6.  
  7.  using namespace std;
  8.  int seed, n, lower, upper, num=0;
  9.  double sum1, sum2, avg, total=0, stn;
  10.  bool repeat = true;
  11.  //function prototypes
  12. void getParameters(int& seed, int& n, int& lower, int& upper);
  13. int getNum(int lower, int upper, int n, int seed);
  14. void update(int num, double& sum1, double& sum2);
  15. void output(double avg, double stn);
  16.  
  17.  int main (){
  18.  
  19.      char input;
  20.      do {
  21.       getParameters(seed,n,lower,upper);
  22.       getNum(lower, upper, n, seed);
  23.       update(num,sum1,sum2);
  24.       output(avg, stn); 
  25.      cout<<endl;   
  26.      cout<<"Enter Y or y to repeat, N or n to exit. ";cin>>input;
  27.      }while(input == 'Y' || input == 'y');
  28.      if (input == 'N' || input == 'n') exit (1);
  29.  system ("pause");
  30.  return 0;
  31.  }
  32.  
  33. /***************************************************************
  34. The function gets the seed, size, upper, and lower bounds used
  35. to generate the random numbers.
  36. ***************************************************************/
  37. void getParameters(int& seed, int& n, int& lower, int& upper){
  38.   system("cls");
  39.   cout<<"Seed for random number generator: ";cin>>seed;
  40.   cout<<"The size of the sequence: ";cin>>n;
  41.   cout<<"Lower and upper bound for the random number:";cin>>lower>>upper;
  42. }
  43.  
  44. /***************************************************************
  45. This function generates the random numbers based on the user input.
  46. ***************************************************************/
  47. int getNum(int lower, int upper, int n, int seed){
  48.     int i, a=0;
  49. cout<<"The numbers generated are: "<<endl; 
  50. srand(seed);   
  51. for (i = 1; i <= n; i++){
  52.     a=(rand()%(upper-lower)+lower);
  53.     total+=a;
  54.     num += (a*a);
  55. cout<<setw(4)<<a;
  56. }
  57. cout<<endl<<endl;
  58. }
  59.  
  60. /***************************************************************
  61. This function adds up all the random numbers (sum1), and adds the
  62. square of the individual numbers(sum2).
  63. ***************************************************************/
  64. void update(int num, double& sum1, double& sum2){
  65.      sum1=total;
  66.      sum2=num;
  67.      }
  68.  
  69. /***************************************************************
  70. This function will output the average and standard deviation
  71. using the values from sum1 and sum2.
  72. ***************************************************************/
  73. void output(double avg, double stn){
  74.      avg = sum1/n;
  75.      stn = sqrt((sum2-(2*avg*sum1)+(n*avg*avg))/n);
  76.      cout.setf(ios::fixed | ios::showpoint);
  77.      cout.precision(2);
  78.      cout<<"Average: "<<avg<<"      "<<"Standard Deviation: "<<stn;
  79.      cout<<endl;
  80.      total = 0;
  81.      num = 0;
  82.      }
  83.  
Oct 10 '11 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

24
by: Stavros Christoforou | last post by:
Hello everyone, I was wondering if someone could help me with an issue I have in C++. I want to select random points within the volume of a sphere. I know how to get random numbers using srand()...
1
by: Intaek LIM | last post by:
generally, we use srand(time(0)) to generate random numbers. i know why we use time(0), but i can not explain how it operates. first, see example source below. ...
9
by: abhi | last post by:
How do you get random numbers in vb .net? I want random six digit numbers. Cheers, Abhi
12
by: Jim Michaels | last post by:
I need to generate 2 random numbers in rapid sequence from either PHP or mysql. I have not been able to do either. I get the same number back several times from PHP's mt_rand() and from mysql's...
1
by: SpecialEd | last post by:
I'm having trouble adding random numbers that I've generated using a for loop. So far my block of code looks: cout<<"Please enter the number of dice you want to use:"; cin>>numberOFdice;...
14
Deathwing
by: Deathwing | last post by:
Hi everyone, I'm fairly new with python and as such am playing around alot with just basic coding/scripting. I've spent the better part of today working with random numbers. Thanks for all your...
12
by: Naya | last post by:
Hi. I am working on a math tutoring program which generates two random numbers (from 1 to 500) and asks the user to add them. How can I check to see whether or not they put in the correct total??...
13
by: Peter Oliphant | last post by:
I would like to be able to create a random number generator that produces evenly distributed random numbers up to given number. For example, I would like to pick a random number less than 100000,...
26
by: bilgekhan | last post by:
What is the correct method for generating 2 independent random numbers? They will be compared whether they are equal. What about this method: srand(time(0)); int r1 = rand(); srand(rand());...
1
PureCoffee
by: PureCoffee | last post by:
So here is some code but I can't get past the generation. I would like to add whatever randomely generated numbers are shown in each "private sub". Obviously I am a NOOB to VB but I rather enjoy...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.