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!