0

I'm in a statistics class and we are constantly being given "dice problems" with various constrains. This is a probability problem, where I need to evaluate the probability of an event by using the Monte Carlo method. I know I could integrate my way through it but I'd like to write a programme that allows me to simply modify constraints in terms of how many dice I have, how many rolls I made and how many sides do those dice have.

This is one of the problems I'm working on. Simpler, cause I don't want to get carried away with my code writing "abilities".

Assume that a dice have 9 sides each. Estimate the probability that when you roll 5 dice at least 3 will have the same value.

This is the general template for the problems we are given: Roll an X number of n-­‐sided dice, each of which has sides numbered from 1 to n. Estimate the probability that we will get 3 or more dice with the same outcome.

I want to write a function for the sample problem, lets say, which takes as input an integer n, which is the number of faces in each die, and computes the probability that 3 or more dice have the same value. My biggest problem is the constraint "at least 3 out of 5". I've looked through other similar problems on Stackoverflow, but none of them really touch base with me. How would you write the code for constraint? I am using Python 3.2.

class Die(object):
  def __init__(self, sides = 9):
    self.sides = sides

  def roll(self):
    return randint(1, self.sides)

I'm stuck here. Any input is helpful, thanks!

jamylak
  • 128,818
  • 30
  • 231
  • 230
user2227808
  • 41
  • 1
  • 9
  • @MartijnPieters Really? But how else would I ensure the dice roll is fair and independent? – user2227808 May 07 '13 at 13:12
  • Ah, and now I should really take my foot out of my mouth and read up on the Monte Carlo method *first*. My apologies, that is the one method where you *do* roll the dice. :-P – Martijn Pieters May 07 '13 at 13:19

2 Answers2

2

I don't think I would use a class here. you just generate the dice roll and then check to see if the dice roll should be counted or not. In this case, I'd use a Counter to do the counting just to make the code a little more clean:

from collections import Counter
from random import randint

def roll(ndice,nsides=9):
    return [randint(1,nsides) for _ in range(ndice)]

def count_it():
    c = Counter(roll(5))
    return c.most_common(1)[0][1] >= 3

ntries = 100000
print (sum(1 for _ in range(ntries) if count_it())/ntries)

It looks to me like you have about a 10% chance. The trick with Monte-Carlo is determining whether you've converged or not. You can do this a few times with different numbers of ntries. The bigger your make ntries, the smaller the spread will be in your output. Eventually, when the spread is small enough, you say you've converged on the solution with some certainty.

mgilson
  • 300,191
  • 65
  • 633
  • 696
  • Thanks for helping out! However, do you mind explaining what you mean by ntries? Is that referring to the number of rolls I make? I see your point as in the more rolls I make the better average I get, but here I am technically assuming I'm only making 1 roll. Would that be 1 try then? – user2227808 May 07 '13 at 14:01
  • @user2227808 -- you can't know anything from 1 try. The basis of the monte carlo method is that you try over and over and you get the probability from actually doing the experiment. So yes, `ntries` is the number of times your computer is rolling 5 9-sided dice. – mgilson May 07 '13 at 14:23
0

Just use your Die class a large number of times:

# roll a lot of dice!
myDie = Die(9) # 9 sides

roll_counts = {side:0 for side in range(1, myDie.sides + 1)} 

numRolls = int(1e6)
for x in xrange(numRolls):
    roll_counts[myDie.roll()] += 1

Then analyze your distribution as needed:

for side in sorted(roll_counts):
    side_pct = float(roll_counts[side]) / numRolls * 100
    print 'side {} comprised {}% of all rolls'.format(side, side_pct)

EDIT: I'm aware this solution doesn't solve your homework problem, but hopefully it gives you the tools you need to roll and count your dice. It's likely you'll need to do the above for multiple die at a time and have a way of comparing their equality at each roll.

mdscruggs
  • 1,182
  • 7
  • 15