-2
import random

sample_size = int(input("Enter the number of times you want me to roll the die: "))

if (sample_size <=0):

    print("Please enter a positive number!")

else:
    counter1 = 0

    counter2 = 0

    final = 0

    while (counter1<= sample_size):

        dice_value = random.randint(1,6)

        if ((dice_value) == 6):
            counter1 += 1

        else:
            counter2 +=1

    final = (counter2)/(sample_size)  # fixing indention 


print("Estimation of the expected number of rolls before pigging out: " + str(final))

Is the logic used here correct? It will repeat rolling a die till a one is rolled, while keeping track of the number of rolls it took before a one showed up. It gives a value of 0.85 when I run it for high values(500+)

Thanks

Yoav Abadi
  • 403
  • 7
  • 16
ggezpython3
  • 53
  • 2
  • 12
  • @PM2Ring Yeah I have edited the code now. – ggezpython3 Sep 29 '18 at 00:53
  • `final = (counter2)/(sample_size)` is not indented correctly. What is the aim of the variable 'final' if it is not used. Where do you define and initialize `expect`? – yoonghm Sep 29 '18 at 00:54
  • @yoonghm it was supposed to be 'final' ( the variable that stores the sum of the number of rolls it took before a 1 showed up) instead of 'expect'. Apologies. – ggezpython3 Sep 29 '18 at 00:56
  • 1
    Could you explain what's your logic of `final = (counter2)/(sample_size)`? – Daveedo Sep 29 '18 at 00:58
  • Ok, that's much better. But there's no point calculating `final` inside the loop if you only use the final `final`. – PM 2Ring Sep 29 '18 at 00:59
  • `final = (counter2)/(sample_size)` is not indented correctly. – yoonghm Sep 29 '18 at 01:00
  • @Daveedo the 'final' variable is supposed to store the sum of the number of rolls before a 1 showed up. – ggezpython3 Sep 29 '18 at 01:00
  • @yoonghm It is edited now. – ggezpython3 Sep 29 '18 at 01:03
  • Shouldn't you be looping until the total number of rolls equals the `sample_size`? – PM 2Ring Sep 29 '18 at 01:05
  • @ggezpython3 Okay. if you would like to counter the # of rolls before a 1 showed up, you can simply use counter2 for it, and add a `break` under the your if statement? I'm still not getting why you put `if (dice_value) == 1` there if you're waiting for a dice_value of 1 but not 6... – Daveedo Sep 29 '18 at 01:06
  • Yes, this is a reasonable solution to this calculation, and yes, it should output you around 0.83 (5/6). – Yoav Abadi Sep 29 '18 at 00:49

2 Answers2

0
import random

while True:
  sample_size = int(input("Enter the number of times you want me to roll a die: "))
  if sample_size > 0:
    break

roll_with_6 = 0
roll_count = 0

while roll_count < sample_size:
  roll_count += 1
  n = random.randint(1, 6)
  #print(n)
  if n == 6:
    roll_with_6 += 1

print(f'Probability to get a 6 is = {roll_with_6/roll_count}')

One sample output:

Enter the number of times you want me to roll a dile: 10
Probability to get a 6 is = 0.2

Another sample output:

Enter the number of times you want me to roll a die: 1000000
Probability to get a 6 is = 0.167414
yoonghm
  • 4,198
  • 1
  • 32
  • 48
0

Sticking with your concept, I would create a list that contains each roll then use enumerate to count the amount of indices between each 1 and sum those, using the indicies as markers.

the variable that stores the sum of the number of rolls it took before a 1 showed up - OP

from random import randint

sample_size = 0
while sample_size <= 0:
    sample_size = int(input('Enter amount of rolls: '))

l = [randint(1, 6) for i in range(sample_size)]

start = 0
count = 0 

for idx, item in enumerate(l):
    if item == 1:
        count += idx - start
        start = idx + 1

print(l)
print(count)
print(count/sample_size)
Enter amount of rolls: 10
[5, 3, 2, 6, 2, 3, 1, 3, 1, 1]
7
0.7

Sameple Size 500:

Enter amount of rolls: 500
406
0.812
vash_the_stampede
  • 4,590
  • 1
  • 8
  • 20