Advertisement
Guest User

coinflips.py

a guest
Mar 7th, 2012
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.81 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
  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 1 for heads if result is below odds
  13. #  or return 0 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.     if no_of_heads == how_many_flips:
  21.         return 1
  22.     else:
  23.         return 0
  24.  
  25. #  choosing a coin pseudo-randomly
  26. #  pseudo-random integer is generated from the number of coins available
  27. #  and compared to the number of fair coins available
  28. #  return "unfair" if it is above the number of fair coins
  29. #  or return "fair" if it is equal to or less than number of fair coins
  30. def choosing_coin(how_many_fair, how_many_unfair):
  31.     total_coins = how_many_fair + how_many_unfair
  32.     chosen_coin = random.randint(1, total_coins)
  33.     if chosen_coin > how_many_fair:
  34.         return "unfair"
  35.     else:
  36.         return "fair"
  37.  
  38. #  ask user for parameters of test
  39. print "Testing probability of fair and unfair coin flips when a random coin is pulled from a bag.\n"
  40. fair_coins = float(raw_input("How many fair coins do you wish to put in the bag? "))
  41. unfair_coins = float(raw_input("How many unfair coins do you wish to put in the bag? "))
  42. unfair_odds = float(raw_input("For the unfair coins, what percent chance does one flip have that it will come up heads? "))
  43. number_of_flips = float(raw_input("How many times do you want to flip the coin chosen from the bag? "))
  44. #  tell the user what the probability is
  45. print "\nThe chances of getting", int(number_of_flips), "heads in a row are about", float("%.4f" % ((((unfair_odds / 100.00) ** number_of_flips) * unfair_coins / (fair_coins + unfair_coins)) + ((.50 ** number_of_flips) * fair_coins / (fair_coins + unfair_coins))))*100.00, "percent."
  46. print "I am now running the simulation 100,000 times. This may take a few seconds.\n"
  47. #  the simulation: success is the number of times the flips came up all heads
  48. success = 0
  49. for x in range(100000):
  50.     #  choose a coin
  51.     if choosing_coin(fair_coins, unfair_coins) == "unfair":
  52.         #  flipping an unfair coin
  53.         if flipping(int(number_of_flips), (unfair_odds)) == 1:
  54.             success += 1
  55.     #  flipping a fair coin
  56.     else:
  57.         if flipping(int(number_of_flips), 50) == 1:
  58.             success += 1
  59. print "Simulation complete. All heads came up", success, "out of 100,000 times."
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement