Compsci 6/101, Fall 2011, Lab 5

Monte Carlo Simulations


A room of computers working at the National Advisory Committee for Aeronautics, 1949. Courtesy of Wikipedia.
"Monte Carlo" simulations are a very powerful technique for calculating values which cannot be calculated exactly. They have been used since the dawn of computing, when scientists on the Manhattan Project were working to design an atomic bomb. Back then, a "computer" was a woman whose job was to do math problems all day long. The nuclear physicists would construct various scenarios of uranium reactions, and then hand these problems off to a room full of computers, similar to a modern data center. Each individual problem didn't explain much about fission, but taking hundreds of scenarios in aggregate allowed the physicists to design the bomb.

There are four parts of this lab:

  1. Reason about list comprehensions (on the handin page)
  2. Reason about how code works
  3. Implement code to test the probabilities in the game of craps
  4. Implement code to test the probabilities in poker

Turn in this page for your group

Snarf the code for this lab (or you can browse online).

Part 0: Thinking about Code

For this problem, reason about the function print_stats in the module HeadsTails.py.
  1. Explain the purpose of the variable rc (an abbreviation for run count): describe its initialization, use, and update.

  2. Explain the purpose of the variable counts, specifically, what does counts[k] represent (in terms of k)?

Part I: Craps and determining Probabilities/Odds

Craps is a dice game played in nearly all casinos. You can play online and you can see what Wikipedia says about the game.

In craps if the player rolls a 7 or 11 on the first/come out roll then you win. If the roll is 2, 3, or 12 then you lose. If the first roll is something else, a more complicated set of bets follows. For this problem you should modify the code in DiceRolling.py so that it prints two statistics: The probability that a 7 or 11 is rolled when two dice are rolled and the probability that 2, 3, or 12 are rolled. Both values should be numbers between 0 and 1. Do this by creating a new function named craps_simulation and calling it at the bottom of the module so that it is the only function executed when the program runs.

  1. Simulate 100,000 dice rolls and write code that determines the number of wins (7 or 11) and the number of losses (2, 3, or 12). If there are 50,000 rolls that are either 7 or 11 then the probability of winning is 1/2 (50,000/100,000). You should strive to write code that does not store the 100,000 rolls, but calculates the probabilities by simulating rolling two dice 100,000 times without storing all the rolls, but by incrementing two different counters/variables appropriately.

Part II: Poker Odds

In this part of the lab, you will be estimating the odds of various hands of poker, specifically a version called 5 card stud, using a Monte Carlo method. The idea is to generate many, many random poker hands, and count which fraction of them are two pairs, full houses, straight flushes, which fraction are four of a kind, etc. See the Wikipedia/Poker Odds page for probabilities/odds.

The program to calculate odds is started in Cardtester.py.

  1. Most of the code provided is for dealing cards, managing hands, and printing the hands. You should look carefully at both get_rank_counts, is_pair, and simulate to see how the provided code determines how many hands contain one pair. Run the program a few times and estimate what percentage of five-card poker hands contain one pair.

  2. Write a function is_two_pair and implement it so that it returns true if a hand contains exactly two pairs. What's the body of the function and the probability/odds for two pairs? You'll need to modify simulate too.

  3. Write a function is_three_of_a_kind to determine when a hand has three of a kind exactly (not four of a kind and not a full house which is three of one rank and two of another rank). For example, [J,J,J,Q,3] is three of a kind, but [3,3,3,8,8] is not because it's a full house.

  4. Write a function is_fullhouse and provide both the code and probability of getting a full house.

  5. Challenge: In poker a flush is all cards of the same suit, e.g., all spades, all hearts, etc. Write code to determine if a poker hand is a flush, simulate to find out how many hands are flushes. This might be an overestimate because a straight flush is better than a flush, but that's an extra challenge.

  6. Challenge: In poker a straight is five cards whose ranks are consecutive, e.g., 2,3,4,5,6 or A,2,3,4,5,6 or 10,J,Q,K,A (where Aces are high or low). Write code to determine if a hand is a straight and simulate to estimate the probability of dealing a straight.