Computer Science Canada

Average of Random Numbers

Author:  whoareyou [ Tue Nov 05, 2013 11:34 am ]
Post subject:  Average of Random Numbers

Hello,

So I recently did an assignment for an introductory programming course (in Python) where we a number of random numbers and count the number of positive, negative, and zero values and then calculate the average. The range of the numbers generated was -max <= integer <= max. What was interesting to me about this problem was that:

Quote:

If the random number generator is good, we would expect the numbers to be generated uniformly across the range. If the number of random numbers generated is large (e.g. 1000) and your program is correct, we would expect the number of positives to roughly equal the number of negatives and the average to be zero in about nine runs out of ten.


The following is sample output the program produced. As you can see, the positive and negative values are pretty much equal to each other and the average is 0.

code:
Enter the number of random numbers to be generated: 1234567890
Enter the maximum integer number to be generated: 50

Sum of the random numbers generated: -1225505
Average of the random numbers generated: -0.0

Number of positive numbers generated: 611152683
Number of negative numbers generated: 611195939
Number of zero values generated: 12219268


Does this result purely from probability (which would mean this would same result would occur in other languages) or is it the way the Python random generator is designed?

Author:  Insectoid [ Tue Nov 05, 2013 12:05 pm ]
Post subject:  RE:Average of Random Numbers

This is a result of probability. Most random number generators (RNG) are designed to produce a uniform distribution of numbers- that is, every number has an equal chance of being picked. There are RNGs that are not uniform- some numbers are more likely to appear than others.


: