Count throws six in a python cube

For an introductory course in Python, I have the task of doing a simulation for eolling dice

You want all your cubes (5 in total) to get the value six, and calculate how many discounts are required for a person to get all the poles. I need a loop that simulates this problem 100,000 times, and then I need to divide the total number of samples by 100,000 to get the result. I know that the final result should be about 13, but I don’t understand this, and I don’t know why.

I know that something is wrong with my approach to this problem, but what?

import random

count1=0
count2=0
count3=0
count4=0
count5=0
loopcounter = 0

for loopcouter in range (1,100000):
    dice1=int( random.random()*6)+1
    if dice1 != 6:
        #reroll
        while dice1 != 6:
            dice1=int( random.random()*6)+1
            #set counter1
            count1 = count1+1
        else:
            count1 = 1

        dice2=int( random.random()*6)+1
        if dice2 != 6:
            #reroll while not six
            while dice2 != 6:
                dice2=int( random.random()*6)+1
                #set counter2
                count2 = count2+1
        else:
            count2 = 1

        dice3=int( random.random()*6)+1
        if dice3 != 6:
            #reroll while not six
            while dice3 != 6:
                dice3=int( random.random()*6)+1
                #set counter3
                count3 = count3+1
        else:
            count3 = 1
        dice4=int( random.random()*6)+1
        if dice4 != 6:
            #reroll while not six
            while dice4 != 6:
                dice4=int( random.random()*6)+1
                #set counter4
                count4 = count4+1
        else:
            count4 = 1

        dice5=int( random.random()*6)+1
        if dice5 != 6:
            #reroll while not six
            while dice5 != 6:
                dice5=int( random.random()*6)+1
                #set counter5
                count5 = count5+1
        else:
            count5 = 1


    #print (dice1)
    print (count1)

#print (dice2)
print (count2)
#print (dice3)
print (count3)
#print (dice4)
print (count4)
#print (dice5)
print (count5)

allcount = count1+count2+count3+count4+count5
averagecount = int(allcount / 100000)

print ("the total number of throws is",allcount)
print ("the average number of throws is",averagecount)

So, if anyone can tell me what I'm doing wrong, that would be great!

+4
3

, , , 6 . :

import random

allcount = 0
for loopcouter in range(100000):   # 1,100000 would only loop 99999 times
    count = [0]*5
    for i in range(5):             # 5 dice
        while True:
            dice = random.randint(1,6)  # Use randint
            count[i] += 1
            if dice == 6:
                break
    allcount += max(count)         # The number of rolls needed to get all 6s

averagecount = allcount // 100000

print("the total number of throws is", allcount)
print("the average number of throws is", averagecount)

12/13.

, , iter lambda while. python ( ):

from random import randint

allcount = 0
for _ in range(100000):
    counts = [1]*5
    for i in range(5):
        dice = list(iter(lambda: randint(1,6), 6))
        counts[i] += len(dice)
    allcount += max(counts)
averagecount = allcount // 100000

, :

allcount = sum(max((1 + sum(1 for _ in iter(lambda: randint(1, 6), 6)))
                   for _ in range(5)) for _ in range(100000))
averagecount = allcount // 100000
+2

.

, , , :

import random

class die(object):
    def __init__(self, sides=6):
        self.sides=sides
        self.count=0

    def roll(self):
        self.count+=1
        return random.randint(1,self.sides)    

    def roll_until(self, tgt, giveup=100000):
        result=0
        self.tgt=tgt
        while result!=tgt and self.count<giveup:
            result=self.roll()
        if self.count<giveup:
            return self.count

( ) , :

>>> d=die()
>>> d.roll_until(6)
2
>>> d.tgt
6
>>> d.count
2

2 - , , 6 6- .

? die:

>>> dice=[die().roll_until(6) for i in range(6)]
>>> dice
[15, 3, 3, 4, 5, 2]

.

:

>>> max(die().roll_until(6) for i in range(6))
9

n , float(n) :

>>> n=100000
>>> sum(max(die().roll_until(6) for i in range(6)) for i in range(n))/float(n)
13.95879

! . :

>>> sum(max(die().roll_until(6) for i in range(5)) for i in range(n))/float(n)
13.0032
+2

0, , while, .

, .

(random.random() * 6) + 1

:

random.randint(1,6)
0

Source: https://habr.com/ru/post/1607165/


All Articles