I have an equation within a while loop like this:

while(repeats != 1000000){

x = rand1;
y = rand2;

result = x + y;
cout << result;

repeats++;
}

Basically, 2 numbers are randomly generated, and are added together to put in the variable 'result'. What the above does is print 1000000 values for result. What I want to do is store every value and take an average at the end, to get an average of the results it gives me.

Any ideas?

Thanks!

>>Any ideas?
You don't have to actually save each individual number in order to calculate the average. But if you want the values for something else then save them in an array or better yet in a file which doesn't take up much RAM.

And my guess is that the sum of all those random numbers may cause data overflow meaning that its impossible to get the average. Your best bet would be to save the sum in a 64-bit integer, such as _int64 or long long.

No I don't have to store each individual number as I will not be using it again, all I need is the average.

So could you possibly explain to me a bit better how to go about getting the average while preventing data overflow? An example would be brilliant so I could apply it to my program. I'm really not sure about how to get averages in c++...

Thanks so much,

Andy.

...and if you could show me using my previous example that would be fantastic :)

Why dont you just remember two things: your average and the number of values in the average. So when you need to add another number you do this:

average*number + yournewnumber divided by number+1

Is that what your looking for?

I'm sorry but that made absolutely no sense to me whatsoever...

I'm all for working things out myself, but I can't get my head around this one I'm afraid...

All you have to so is sum up all the random numbers and divide the total by however numbers were generated. Its 4th grade math. The last line below is the average.

long long sum = 0;
srand(time(0));
long repeats = 0;
while(repeats < 1000000){

x = rand();
y = rand()

result = x + y;
sum += result;
cout << result << "\n";

repeats++;
}
cout << "Average = " << sum / (repeats * 2);

To add another number to an average you need to know 2 things. The Average and the Number of Values. The Average is calculated as such: SUM/NUMBERofVAL=AVERAGE. So when you multiply AVERAGE by NUMBERofVAL you will get the sum. Then to add another number you simply add the next number then divide by the new number of values computed in the average. Know what I'm saying. Think of it like this.

9+3+4+4=20
20/4=5 which is your average. Now you dont need to know that there was a 9 3 4 4. All you need to know is there there were 4 numbers. So 5 (average) * 4 (numberofvalues) = 20 your sum. Now add your new number lets say 5 and divide by the new numberofvalues which is 5. So your average is 5!

my program REALLY doesnt like that += thing you did...

it runs the program but crashes? It gives an error to that line.

my program REALLY doesnt like that += thing you did...

it runs the program but crashes? It gives an error to that line.

Post your code. You have to fix up all the other errors that are in your code such as declaring variables. After doing that your program compiles ok for me using VC++ 2008 Express.

>>that += thing you did
That is just shorthand for sum = sum + result; The two are identical, but you will find youself using += as you gain more experience to save keystrokes.

> Basically, 2 numbers are randomly generated, and are added together
> to put in the variable 'result'. ...What I want to do is store every value and
> take an average at the end, to get an average of the results it gives me.

using rand() , each pseudo random number generated is in the range 0 to RAND_MAX. result would vary (non-uniformly) between 0 and RAND_MAX*2 with a mean of RAND_MAX. by the central limit theorem, we can replace this averaging by taking a single sample from the normal distribution. http://en.wikipedia.org/wiki/Central_limit_theorem (almost always it would be very very close to the mean RAND_MAX. the variace of the average would be only one millionth of the variance of the original distribution.)

Maybe that's true in theory but it doesn't work out very well. Here are 10 random numbers and none of them except 1 are very close to RAND_MAX, and the average isn't even close.

RAND_MAX = 32767
23510
8870
11358
32748
20883
23642
20162
18820
1215
11317
avg = 17252
Press any key to continue . . .

But if I let the program get 1000000 random numbers the average is almost exactly in the middle of RAND_MAX. But I think for the purposes of the OPs problem almost isn't good enough.

'result' in the original problem is the sum of two random numbers.
and we are talking about the average value of a large number of samples, not any particular value.

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

int main()
{
  std::cout << "RAND_MAX: " << RAND_MAX << '\n' ;
  std::srand( std::time(0) ) ;
  for( int i=0 ; i<10 ; ++i )
  {
    enum { N = 1000000 } ;
    double sum = 0.0 ;
    for( int i=0 ; i<N ; ++i )
      sum += std::rand() + double( std::rand() ) ;
    std::cout << std::fixed << sum/N << '\n' ;
  }
}
RAND_MAX: 2147483647
2146112696.355224
2148536425.521353
2147638775.226065
2145940738.317371
2149102448.850054
2148997404.831785
2148572188.735253
2147364959.489652
2146814381.954062
2147781874.107042

Here is the program I used using VC++ 2008 Express. And why is the value of RAND_MAX so much larger than mine ???

Its not my understanding that the OP is taking 10 samples and each sample size is 1000000 * 2 random numbers. But I see what you are attempting to prove is that the average is 2000000 random numbers is very close to the same each time it is calculated. I found that also, but posted only one sample, not 10. My first post was just simply 10 random numbers. The second post was the average of 1000000 random numbers.

#include <ctime>
#include <iostream>
using namespace std;
const long maxcount = 1000000;
int main()
{ 
    int i;
    _int64 sum = 0;
    srand((int)time(0));
    cout << "RAND_MAX = " << RAND_MAX << "\n";
    for(i = 0; i < maxcount; ++i)
    {   
        long x = rand();
        sum += x;
    //    cout << x << "\n";
    }
    _int64 avg = sum / maxcount;
    cout << "avg = " << avg << "\n";
        
    return 0;    

}

> why is the value of RAND_MAX so much larger than mine ???
microsoft has always used a 16-bit value for computing rand(). as the generarator is a linear congruential one, this makes computation very fast. (multiplication can be done in a 32-bit register and the new value extracted by bit manipulation on the result). gcc uses 32-bit random numbers. perhaps, the microsoft implementation would change (with 64-bit processors becomming more common).

> But I think for the purposes of the OPs problem almost isn't good enough.
agreed. but only one random sample (from a normal distribution) needs to be generated.

#include <iostream>
#include <boost/random.hpp>
#include <cstdlib>

int main()
{
  boost::mt19937 rng ;
  boost::normal_distribution<> distr( RAND_MAX ) ;
  boost::variate_generator<boost::mt19937&,
        boost::normal_distribution<> > generator( rng, distr ) ;
  std::cout << std::fixed << generator() << '\n' ;
}
This article has been dead for over six months. Start a new discussion instead.