Simple query: sum of random integers

Hi there!

I've just started trying to learn C++ and am using the book "C++ by dissection"

Been trying to generate a list of 100 random integers and its corresponding sum but i'm quite certain that the sum i get is wrong (only 9 digits vs the 10 digits of the random integers). Any help would be appreciated!

#include <iostream>
#include <math.h>

using namespace std;

int main()
{
int how_many = 100;
int sum;

cout << "Print " << how_many
<< " random integers." << endl;
for (int i = 0; i < how_many; ++i)
{
cout << rand() << '\t' << '\t';
cout << endl;
sum = sum + rand();
}

cout << "the sum is " << sum << endl;
}

I'm guessing that my equation "sum = sum + rand()" is wrong but i don't know why.

Thank a bunch!
15
16
17
cout << rand() << '\t' << '\t'; // get and print a random number  (why those tabs?)
cout << endl;
sum = sum + rand(); // here you will get a new random number, different from that you have previously printed on screen 
Do you not know that the sum of random numbers is a random number? ;-)
You are experiencing arithmetic overflow.

(Since rand() can return any number in the full range of int.)
Wouldn't the value of rand in cout << rand() << // ... be different to the value of rand in sum = sum + rand();?
I agree with jsmith.. There's nothing wrong with your equation, but sum(as an integer) can't hold the potential huge number that you get.. Instead you get a garbage value. Try declaring sum as a double. That should solve the problem
Try declaring sum as a double. That should solve the problem


Sort of.

You'd be better off using a larger integer type.

Although I thought RAND_MAX was only 65535? Does it depend on implementation?
RAND_MAX is defined as INT_MAX I believe (without looking).

But use of a larger integral type is best, if available.
Hi Bazzy!

thanks! i understand the problem now.

so what should i do if i want to sum the random numbers which i had previously printed on the screen?

Thanks again in advance!
opps... ok...

so apparently there are 2 solutions to the problem

1. i only need to define sum to hold a larger value
2. rand() in "sum = sum + rand()" gives me a new set of integers different from that of the previous rand() that was printed

anybody able to clarify?

Thanks!

And i'm completely new to this forum.. this place rocks! answers come in so quickly!

hmm... i tried running it with only 2 random numbers and my sum != the sum of my printed numbers

that means that rand() provide a new set.

how do i rectify this problem? can i call on the numbers printed previously?

thanks!
I guess one way to do it is to use a temporary variable, which will save the value that rand() returns. This way you'll be able to monitor rand()'s and get the sum you want.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <math.h>

using namespace std;

int main()
{
int how_many = 100;
long int sum = 0;
int temporary;

cout << "Print " << how_many 
<< " random integers." << endl;
for (int i = 0; i < how_many; ++i)
{
temporary = rand();
cout << temporary << endl;
sum = sum + temporary;

}

cout << "the sum is " << sum << endl;
return 0;
}
ah =)

that does make sense, and it appears to work quite well.

Any other suggestions are also welcome!


Thanks fission!
Um, you do know, even accounting for arithmetic overflow, that the sum of random numbers is a random number?

It just seems like a lot of work to get a single random number... that's all.
It is worth noting that if X and Y are uniformly distributed (discrete or continuous) random
variables across ranges Rx and Ry, regardless of whether Rx == Ry, the sum X + Y is not
uniformly distributed.
Hi Duoas!

Haha. Yup! I do know its a random number =)

I'm trying to learn the basics of c++ on my own, and was working through the exercises of a text. So i'm not too concerned about the applicability at the moment, but as to how the system works!

that said.. the sum of a list of random numbers is made up of the numbers on the list. So if i can list out the random numbers the sum shouldn't be random any more right?

Thanks!


Hi jsmith!

i didn't know that the sum of X + Y is not uniformly distributed, though, once i think about it.. it makes perfect sense.. lol

What then is the distribution of a sum of uniformly distributed random numbers?

Would you have an online resource you could point me towards?

Many Thanks!
selflearning wrote:
What then is the distribution of a sum of uniformly distributed random numbers?


It is also not evenly distributed. For an example, go look at a chart of possibilities of rolling 2 dies. 7 has a very high chance compared to say, 2.
Heh heh heh... Just having fun with this.
Topic archived. No new replies allowed.