Advertisement
dmveazey

coinflips_with_decimals.py

Mar 8th, 2012
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.80 KB | None | 0 0
  1. #  coinflips.py
  2. #  a program to test the concept presented at
  3. #  http://www.khanacademy.org/math/probability/v/dependent-probability-example-1
  4. #  by Daniel Veazey <danielveazey@gmail.com>
  5.  
  6. import random, decimal
  7.  
  8. #  what happens when you flip a coin a number of times, depending on
  9. #  whether the coin is fair or unfair (weighted)
  10. #  a pseudo-random number is genereated from 1 to 100
  11. #  and compared to what its odds of landing on heads are
  12. #  return True for heads if result is below odds
  13. #  or return False for tails if the result is above the odds
  14. def flipping(how_many_flips, odds):
  15.     no_of_heads = 0
  16.     for flip in range(0, how_many_flips):
  17.         flip_result = random.randint(1, 100)
  18.         if flip_result <= odds:
  19.             no_of_heads += 1
  20.     return no_of_heads == how_many_flips
  21.  
  22. #  choosing a coin pseudo-randomly
  23. #  pseudo-random integer is generated from the number of coins available
  24. #  and compared to the number of fair coins available
  25. #  True = unfair coin; False = fair coin
  26. def choosing_coin(how_many_fair, how_many_unfair):
  27.     total_coins = how_many_fair + how_many_unfair
  28.     chosen_coin = random.randint(1, total_coins)
  29.     return chosen_coin > how_many_fair
  30.  
  31. #  ask user for parameters of test
  32. print "Testing probability of fair and unfair coin flips when a random coin is pulled from a bag.\n"
  33. fair_coins = decimal.Decimal(raw_input("How many fair coins do you wish to put in the bag? "))
  34. unfair_coins = decimal.Decimal(raw_input("How many unfair coins do you wish to put in the bag? "))
  35. unfair_odds = decimal.Decimal(raw_input("For the unfair coins, what percent chance does one flip have that it will come up heads? "))
  36. number_of_flips = decimal.Decimal(raw_input("How many times do you want to flip the coin chosen from the bag? "))
  37.  
  38. #  tell the user what the probability is
  39. probability = decimal.Decimal((((((unfair_odds / decimal.Decimal('100.00')) ** number_of_flips) * unfair_coins / (fair_coins + unfair_coins)) + ((decimal.Decimal('.50') ** number_of_flips) * fair_coins / (fair_coins + unfair_coins))))*decimal.Decimal('100.00')).quantize(decimal.Decimal('.01'))
  40. print "\nThe chances of getting", int(number_of_flips), "heads in a row are about", probability, "percent."
  41. print "I am now running the simulation 100,000 times. This may take a few seconds.\n"
  42.  
  43. #  the simulation: success is the number of times the flips came up all heads
  44. success = 0
  45. for x in range(100000):
  46.     #  choose a coin
  47.     if choosing_coin(fair_coins, unfair_coins) == True:
  48.         #  flipping an unfair coin
  49.         if flipping(int(number_of_flips), (unfair_odds)) == True:
  50.             success += 1
  51.     #  flipping a fair coin
  52.     else:
  53.         if flipping(int(number_of_flips), 50) == True:
  54.             success += 1
  55. print "Simulation complete. All heads came up", success, "out of 100,000 times."
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement