#!/usr/bin/env python #-------------------------------------------------------------------- # lawlarge.py # # This program will illustrate the LAW OF LARGE NUMBERS using # a computer simulation that repeatedly tosses a 'fair' coin. # # execute using: $ python lawlarge.py # # programmer: ALLAN CRUSE # written on: 12 JAN 2011 #-------------------------------------------------------------------- import random print "\n\n %9s " % " ", print "Tossing a coin to illustrate the Law of Large Numbers" print for demo in range(3): heads = 0 tails = 0 tosses = 0 for rep in range(100000): x = random.randint(0, 1) if ( x > 0 ): heads += 1 else: tails += 1 tosses += 1; if tosses in [10, 100, 1000, 10000, 100000]: proportion = (1.0*heads) / tosses print " After %d tosses:\t" % tosses, print "%6d heads" % heads, print "%6d tails" % tails, print " proportion of heads=%0.2f" % proportion print print