#!/usr/bin/python
import random
import math
def fair_die():
"""
Roll a fair die and return its outcome (an integer between 1
and 6, inclusive).
"""
return math.ceil(random.random() * 6)
def n_fair_dice(n):
"""
Roll n fair dice and return an array of their
outcomes.
"""
return [int(fair_die()) for x in xrange(n)]
def one_or_five():
"""
Roll 6 fair dice. Return True iff there's a
one or a five among the n outcomes.
"""
return any([x in [1,5] for x in n_fair_dice(6)])
def one_or_five_n_trials(n):
"""
Run n trials of the "roll 6 fair dice" experiment,
and count the number of trials in which at least one
of the dice is a one or a five.
"""
# TODO: do this in a sexier list-comprehension
# way.
one_or_five_count = 0
for i in xrange(0,int(n)):
if one_or_five():
one_or_five_count += 1
return one_or_five_count
def main():
# Run 10,000,000 of the "ones or fives" experiments.
n = 1e7
true_count = one_or_five_n_trials(n)
prob = float(true_count)/float(n)
print prob
# TODO:
# 1. Write some unit tests. About 1/6th of the fair_die()
# calls should return 1s, 1/6 should return 2s, etc.
# 2. Too many of the idioms in here are serial. Parallelize
# them.
if __name__ == '__main__':
main()