l.m
l.m

Reputation: 131

Printing the probability when a die rolls all 6 sides

I am trying to print the expected amount of when a fair die is rolled and to keep rolling until all 6 sides are rolled.

What I am trying to do is when as an example a 1 is rolled it is added to a list. Then to keep going till all 6 sides are rolled and using count+=1 to keep rolling the next die and use count as the number of times. Then once the list equals [1,2,3,4,5,6] to make stop equal True and to break.

But should I use a shift method so if 3 is found then you delete it from the list?

Then once done at the end I want to compute the expected amount of rolls using count

import random
def rolldice():
    count = 0
    while True:
        die = []
        win = []
        for i in range(1):
            die.append(random.choice([1,2,3,4,5,6]))
        stop = False
        for roll in die:
            number = die.count(roll)
            if(number == 1):
                win += [1]
            if(number == 2):
                win += [2]
            if(number == 3):
                win += [3]
            if(number == 4):
                win += [4]
            if(number == 5):
                win += [5]
            if(number == 6):
                win += [6]
            if(win == [1,2,3,4,5,6]):
                stop = True
                break
        if stop:
            break
        else:
            count += 1
    print(f'Count is {count}')
def main():
    rolldice()
main()

Trying to see if I am on the right track or if I should use shifting and deleting.

Upvotes: 1

Views: 296

Answers (1)

John Gordon
John Gordon

Reputation: 33335

If you just want to count how many rolls it takes to get all six sides, using a set is much easier:

import random

# initialize to empty set
results = set()

# remember how many rolls we have made
rolls = 0

# keep going until we roll all six sides
while len(results) != 6:
    rolls += 1
    results.add(random.choice([1,2,3,4,5,6]))

print('It took %d rolls to get all six sides' % rolls)

Upvotes: 2

Related Questions