views:

171

answers:

4

As an exercise I'm writing a program to calculate the odds of rolling 5 die with the same number. The idea is to get the result via simulation as opposed to simple math though. My program is this:

# rollFive.py

from random import *

def main():
    n = input("Please enter the number of sims to run: ")
    hits = simNRolls(n)
    hits = float(hits)
    n = float(n)
    prob = hits/n
    print "The odds of rolling 5 of the same number are", prob

def simNRolls(n):
    hits = 0
    for i in range(n):
        hits = hits + diceRoll()
    return hits


def diceRoll():
    firstDie = randrange(1,7,1)
    for i in range(4):
        nextDie = randrange(1,7,1)
        if nextDie!=firstDie:
            success = 0
            break
        else:
            success = 1
    return success

The problem is that running this program with a value for n of 1 000 000 gives me a probability usually between 0.0006 and 0.0008 while my math makes me believe I should be getting an answer closer to 0.0001286 (aka (1/6)^5).

Is there something wrong with my program? Or am I making some basic mistake with the math here? Or would I find my result revert closer to the right answer if I were able to run the program over larger iterations?

+5  A: 

The probability of getting a particular number five times is (1/6)^5, but the probability of getting any five numbers the same is (1/6)^4.

There are two ways to see this.

First, the probability of getting all 1's, for example, is (1/6)^5 since there is only one way out of six to get a 1. Multiply that by five dice, and you get (1/6)^5. But, since there are six possible numbers to get the same, then there are six ways to succeed, which is 6((1/6)^5) or (1/6)^4.

Looked at another way, it doesn't matter what the first roll gives, so we exclude it. Then we have to match that number with the four remaining rolls, the probability of which is (1/6)^4.

jbourque
(1/6) XOR 5 ???
John Machin
No, (1/6) to the power of 5. I'm more used to **, e.g. x**y, but I went with the notation that was in the question.
jbourque
+1  A: 

Your math is wrong. The probability of getting five dice with the same number is 6*(1/6)^5 = 0.0007716.

Peter
A: 

I think your expected probability is wrong, as you've stated the problem. (1/6)^5 is the probability of rolling some specific number 5 times in a row; (1/6)^4 is the probability of rolling any number 5 times in a row (because the first roll is always "successful" -- that is, the first roll will always result in some number).

>>> (1.0/6.0)**4
0.00077160493827160479

Compare to running your program with 1 million iterations:

[me@host:~] python roll5.py 
Please enter the number of sims to run: 1000000
The odds of rolling 5 of the same number are 0.000755
dcrosta
+1  A: 

Very simply, there are 6 ** 5 possible outcomes from rolling 5 dice, and only 6 of those outcomes are successful, so the answer is 6.0 / 6 ** 5

John Machin