Recently I discovered the random.randint() function in python. Basically you call it with 2 ints, a low value and a high value. It will return a integer in that range (inclusive). I was playing around with it and I thought it seemed to be giving me the same number awfully often, so I whipped up a test: call that method 1 million times, record the values, then repeat 6 times.
I’m using randint() to simulate dice so I’m curious to see if the number distribution is even across the numbers 1 through 6. Below is my test code:
for x in range(6): counts = [0,0,0,0,0,0,0,0] for x in range(ONE_MILLION): counts[d6()] += 1 for i in counts: print i, ',', print ''
Each time d6() (my wrapper around randint()) is called, it returns a number 1-6. This is used as a look up into the counts list, and the number there is incremented by one. I have 0′s on both sides of the 1-6 slots just to make sure it really is returning a correctly bounded value. The numbers in each row should sum up to 1 million.
By running this 6 times, I should get an idea of where the numbers are falling to make sure there is an even distribution. (Truly random numbers will have an average distribution over the long term, if they are grouping around one number, then they random number generator is not doing a good job.) I took the total of each column (which should be very close to 1 million) and then found the percent error ( ((amount – expected) / expected) *100) (omitting the absolute values that are usually used). The average of the percent errors was 0. This leads me to believe that the distribution of random numbers generated by the randint() function are sufficiently random for my uses.
Now that I have stated this, I have no more excuses but to continue on with coding the game that will use said function in a dice throwing function. Below is the spreadsheet of my data as generated by Google Spreadsheets.
By the way, this data was generated with python 2.4.3.