Craps

November 4, 2011

Craps is a gambling game played with two dice. The “shooter” begins the game by rolling two dice. If the pips on the dice total 2, 3 or 12, the shooter loses. If the pips on the dice total 7 or 11, the shooter wins. If the pips on the dice total 4, 5, 6, 8, 9, or 10, the total of the pips establishes the “point” and shooter continues to roll until he rolls the point, when he wins, or he rolls a 7, when he loses.

Your task is to write a program that simulates a game of craps. Use the program to calculate various statistics: What is the most common roll? What is the average winning percentage? What is the average number of rolls in a game? The maximum? Or you can make your own questions. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.

Wow, that might be the first R solution I’ve seen posted here. Makes sense, though, if you’re working with random variates and statistics. If there’s a way to vectorize the code, I don’t see it; so far the only thing I came up with was roll <- sum(sample(1:6, 2, replace=T)) (which isn’t very impressive).

Do you guys see GDean’s R code as properly indented? For me it’s flush left in both Chromium and Firefox.
This happened to me previously even though I used the advertised sourcecode tags that used to work.

The HTML source for the R code is indented but the whitespace is not preserved.

I fixed the comment formatting. I changed &lt; and &quot; to their ascii equivalents, added a [sourcecodx lang=”css”] to the top of the code, and [/sourcecodx] at the bottom of the code (note that I purposely mangled the word sourcecodx to prevent WordPress from writing my comment incorrectly). Looks fine to me.

Jussi: Only a small number of languages are directly supported by the sourcecodx tag. You may have said something like [sourcecodx lang=”fortran”]. I just looked at the WordPress support page linked in the HOWTO, and it appears they have added more languages since I wrote the HOWTO, and also added a feature for “plain text” source code: just say [sourcecodx] with no language parameter. I just checked by editing GDean’s code — that works, too. It’s too bad WordPress doesn’t support the <pre> tag.

Graham: Unless the code is extremely long, it is better to have it here than elsewhere. In the first place it is more easily seen (I checked the stats — 110 people saw your comment, but only 9 clicked the link), and in the second place you can interrupt the code with annotations, as I frequently do in my suggested solutions.

It doesn’t look like you mistyped anything. I changed “lang” to “language” and it worked fine.

I’ve seen this problem before, and don’t know how to deal with it. Sometimes “lang” works, and sometimes “language” works. I have no idea why.

The workaround is probably to just ignore the language parameter and bracket your code with [sourcecodx] … [/sourcecodx] tags. You will lose the clever language-based formatting, but that should work with anything.

The other approach is to format your code for html using the “foo-to-html” translator on the HOWTO page. That’s what I do for all the code presented in the suggested solutions. It’s inconvenient, but makes the code more integral to the running text.

Private emails to me have one overwhelming complaint: readers can’t edit their own comments. I’ve considered moving from the WordPress comment forums to DISQUS, but haven’t done it because managing one blog is hard enough, and managing two things would doubtless be harder. Does anyone have experience with DISQUS?

The HOWTO page seems to suggest that HTML code tags work in this comment box. I just experimented with HTML style attributes, where style="white-space: pre" should preserve all whitespace. It worked in my tests.

If this comment box allows both the code tags and the attribute, the following test code appears properly indented without any special markup in the code itself:

I think it stripped the attribute out. Just one repeat to test that I did not forget to type it in. And why would it preserve linebreaks then? Also, I’m now replacing the less-than signs with the entity, which I did not do above.(define (gcd m n)
(cond ((< m n) (gcd m (- n m)))
((< n m) (gcd (- m n) n))

Ok, it seems like specifying the white-space style as pre or pre-wrap for the code element should work since the software recognizes the code tags. It does not preserve the style attribute, however, and I failed to find out how it manages to obey line breaks in code while ignoring indentations.

Wait. I did not fail to find out. It adds br-elements at line breaks inside code elements. It’s doing too much and too little at the same time. So we continue to indent the hard way. Ok.

When I first started the blog, I spent a day experimenting, as you have done. I’m pretty sure the choices are what I listed in the HOWTO, except that now WordPress seems to handle a [sourcecodx] . . . [/sourcecodx] without a language designator as plain text, which it did not do back then. I’ll add that to the HOWTO.

Here’s some on-topic code, indented with sed as in the HOWTO page.
The rules of the game are encoded in a generator that produces one
game.
>>> seed(exp(1))
>>> list(game())
[(6, None), (9, None), (4, None), (4, None), (6, 'win')]

The log odds (meant to be to the base that Turing and Good used)
thing is sometimes positive, sometime negative, which seems to
suggest that the game is more or less fair.
>>> logodds(100)
0.17374096069422723
>>> logodds(100)
-0.6963592814139442

This is Python 3. The magic keyword is yield. A couple of the generator things below produce indefinitely long sequences. In theory, game could do so but in practice it of course never does. It is safe to ask for the next game, or the next grade, or the 100 next games as above.
from random import seed, randrange
from math import exp, log10
from itertools import islice