Probabilistic exercises return different results that are expected

As an exercise, I am writing a program to calculate the chances of rolling 5 with the same number. The idea is to get results through simulation, not simple mathematics. My program is like 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 between 1,000,000 gives me a probability of usually between 0.0006 and 0.0008, while my math leads me to believe that I should get an answer closer to 0.0001286 (aka (1/6) ^ 5).

Is there something wrong with my program? Or am I doing some basic mistakes with the math here? Or will I find that my result will come back closer to the correct answer if I manage to run the program through larger iterations?

+2


source to share


4 answers


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

There are two ways to see this.



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

I looked differently, no matter what the first video gives, so we exclude it. We then have to match this number with the four remaining shafts, which have a probability of (1/6) ^ 4.

+6


source


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

.



+1


source


Quite simply, there are 6 ** 5

possible results from rolling 5 dice, and only 6 of those results are successful, so the answer is6.0 / 6 ** 5

+1


source


I think your expected probability is wrong as you have stated the problem. (1/6) ^ 5 - the probability of rolling a certain amount of 5 times in a row; (1/6) ^ 4 - 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 a certain number).

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

      

Compare this 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

      

0


source







All Articles