Computer Science Canada Programming C, C++, Java, PHP, Ruby, Turing, VB   Username:   Password: Wiki   Blog   Search   Turing   Chat Room  Members
Average of Random Numbers
Author Message
whoareyou

Posted: 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?

Insectoid

Posted: 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.
 Display posts from previous: All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First

Page 1 of 1  [ 2 Posts ]
 Jump to:  Select a forum  CompSci.ca ------------ - Network News - General Discussion     General Forums   -----------------   - Hello World   - Featured Poll   - Contests     Contest Forums   -----------------   - DWITE   - [FP] Contest 2006/2008   - [FP] 2005/2006 Archive   - [FP] 2004/2005 Archive   - Off Topic     Lounges   ---------   - User Lounge   - VIP Lounge     Programming -------------- - General Programming     General Programming Forums   --------------------------------   - Functional Programming   - Logical Programming   - C     C   --   - C Help   - C Tutorials   - C Submissions   - C++     C++   ----   - C++ Help   - C++ Tutorials   - C++ Submissions   - Java     Java   -----   - Java Help   - Java Tutorials   - Java Submissions   - Ruby     Ruby   -----   - Ruby Help   - Ruby Tutorials   - Ruby Submissions   - Turing     Turing   --------   - Turing Help   - Turing Tutorials   - Turing Submissions   - PHP     PHP   ----   - PHP Help   - PHP Tutorials   - PHP Submissions   - Python     Python   --------   - Python Help   - Python Tutorials   - Python Submissions   - Visual Basic and Other Basics     VB   ---   - Visual Basic Help   - Visual Basic Tutorials   - Visual Basic Submissions     Education ----------- - Student Life   Graphics and Design ----------------------- - Web Design     Web Design Forums   ---------------------   - (X)HTML Help   - (X)HTML Tutorials   - Flash MX Help   - Flash MX Tutorials   - Graphics     Graphics Forums   ------------------   - Photoshop Tutorials   - The Showroom   - 2D Graphics   - 3D Graphics     Teams ------ - dTeam Public

 Style: Appalachia blueSilver eMJay subAppalachia subBlue subCanvas subEmjay subGrey subSilver subVereor Search: