Advertisement
Guest User

Untitled

a guest
Feb 20th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.21 KB | None | 0 0
  1. #craps.py -- Simulates multiple games of craps
  2. # and estimates the probability that the player wins.
  3.  
  4. import random
  5.  
  6. def output(wins, total):
  7. winrate = wins / total
  8. print("The player won {0} of {1} games ({2:0.2%})".format(wins, total, winrate))
  9.  
  10. def rolldies():
  11. dice1 = random.randint(1, 6)
  12. dice2 = random.randint(1, 6)
  13. roll = dice1 + dice2
  14. return roll
  15.  
  16. def simOneGame():
  17. initial_roll = rolldies()
  18. if initial_roll == 2 or initial_roll == 3 or initial_roll == 12:
  19. return True # won
  20. elif initial_roll == 7 or initial_roll == 11:
  21. return False #lost
  22. else:
  23. #Roll until roll is 7 or initial roll
  24. roll = rolldies()
  25. while roll != 7 and roll != initial_roll:
  26. roll = rolldies()
  27. if roll == 7:
  28. return True #won
  29. else:
  30. #roll is inital_roll
  31. return False #lost
  32.  
  33. def simNGames(games_to_sim):
  34. wins = 0
  35. for i in range(games_to_sim):
  36. if simOneGame():
  37. wins += 1
  38. return wins
  39.  
  40. def main():
  41. games_to_sim = int(input("How many games to simulate? "))
  42. wins = simNGames(games_to_sim)
  43. output(wins, games_to_sim)
  44.  
  45. if __name__ == "__main__":
  46. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement