I'm kinda new to python and I would appreciate your help :) I have the following code :
import random
x = 1
def round():
return random.randint(1,6)
for i in range(8):
result = round()
if result > x:
x = result
if x == 5:
print "True"
The goal is to calculate the probability of the program to print "True", The probability should be 0.193. What would be the most effective way with maximum efficiency to do it? I thought about something related to bernoulli distribution and tried but my result was wrong. Thanks!
The first step is to realize that your program simplifies to
x = max(randint(1,6) for _ in range(8))
then the odds that x == 5 is
prob(nothing higher than 5) - prob(nothing higher than 4)
which is
(5/6)**8 - (4/6)**8 # => 0.19354959705075458
If I got your question right, you want to understand how randomint actually generates the numbers (the distribution). It looks that it generates them in an uniform way (you have approximately the same chance of getting any number in the interval).
More details can be found here, here and even more "nerdish" here.
Thus, Hugh Bothwell's answer is correct (it is based on the uniform distribution of numbers you get).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With