Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python probability

We have a six sided die, with sides numbered 1 through 6. The probability of first seeing a 1 on the n-th roll decreases as n increases. I want to find the smallest number of rolls such that this probability is less than some given limit.

def probTest(limit):
    prob = 1.0
    n = 1
    while prob > limit:
        prob = (1/6)**n
        n += 1        
    return n-1

What is wrong with my code?

like image 648
Jamol Avatar asked Feb 03 '26 06:02

Jamol


2 Answers

The probably of rolling a one on the nth roll is 5/6^(n-1)*1/6, not 1/6^n.
1/6^n is the probability of rolling one on all n rolls.

The first n-1 rolls each have a 5/6 chance of not being one.
The nth roll has a 1/6th chance of being one.

like image 76
Jeff Avatar answered Feb 05 '26 18:02

Jeff


the correct will be: prob = (5.0/6)**(n-1)*1/6.0

like image 42
jammy Avatar answered Feb 05 '26 18:02

jammy