Grace Cheng
Grace Cheng

Reputation: 59

roll dice in Python

I need to find expectation of the number of 100 times that I need to roll a fair die until I get all six numbers

And here is my code. I don't know how to put it into while loop for 100 times. Can anyone help?

for example: the first time I rolled to get all six numbers, I need to roll 8 times. the second time I rolled to get all six numbers, I roll 13 times Then I record as follows: 1: 8 2: 13 ... 100:9

I want my a=[8,13,.....,9]

import random
trials = []
a=[]
collection = [random.randint(1,6) for x in range(6)]
for c in collection:
    if c not in a:
        a.append(c)
    
    else:
        collection.append(random.randint(1,6))
trials.append(len(collection))

print(a)
print(collection)
print(trials)

Upvotes: 4

Views: 1018

Answers (4)

ssp
ssp

Reputation: 1710

Does the following work as wanted?:

from random import randint

trials = []
goal = set(list(range(1, 7)))
for _ in range(100):
    attempts = 0
    cur = set()
    while cur != goal:
        cur.add(randint(1, 6))
        attempts += 1
    trials.append(attempts)

Upvotes: 0

ppwater
ppwater

Reputation: 2277

You can loop it like this:

import random
n = 1
trials = []
while n<=100:
    a=[]
    collection = [random.randint(1,6) for x in range(6)]
    for c in collection:
        if c not in a:
            a.append(c)
        
        else:
            collection.append(random.randint(1,6))
    trials.append(len(collection))
    n += 1
    print(a)
    print(collection)
    print(trials)

Just do n = 1 and while n <= 100 and n += 1

And here is the average code:

import random
n = 1
trials = []
while n<=100:
    a=[]
    collection = [random.randint(1,6) for x in range(6)]
    for c in collection:
        if c not in a:
            a.append(c)
        
        else:
            collection.append(random.randint(1,6))
    trials.append(len(collection))
    n += 1
    #print(a)
    #print(collection)
    #print(trials)
print(sum(trials) / len(trials))

Upvotes: 0

Patrick Artner
Patrick Artner

Reputation: 51633

Algo:

  • create empty dict for results
  • do 100 times
    • create set of numbers 1-6
    • create empty list of this tries drawn numbers
    • while there are still numbers in the set repeat:
      • draw a number and add it to your list
      • remove number from set if in it
    • add list to dict
  • evaluate stuff

Or in code:

import random

results = {}

for t in range(100):
    nums = set((1,2,3,4,5,6))
    data = []
    # do as long as we still lack one number
    while nums:
        n = random.randint(1,6)
        data.append(n)
        # remove if in, else we dont care
        if n in nums:
            nums.remove(n)
    # add the list to your results - if you just need the lenghts don't
    # store the whole list here, just store its lenghts and adapt the 
    # evaluation code
    results[t] = data

# evaluation
lengths = [len(w) for w in results.values()]

average_lengths = sum(l for l in lengths) / 100
max_lengths = max(lengths)
min_lengths = min(lengths)

# output
print(min_lengths, average_lengths, max_lengths)

Output:

6 13.95 31 

You can print the dictionary results to see all dices rolled.
You can print the list lenghts to see all 100 varying lengths.

Upvotes: 2

zoomlogo
zoomlogo

Reputation: 176

Just wrap the for loop in another loop:

for i in range(100):
    a = []
    for c in coll:
        if c not in a:
            a.append(c)
        else:
            coll.append(random.randint(1, 6))
    trials.append(len(coll))

Upvotes: -1

Related Questions