Saturday, November 17, 2007

Adventures in Probability (or, Why I Did Not Gamble in Reno)

As you might surmise, I made it back safe and sound from Reno last night. I had a good time at the conference, but I am very glad to be home.

Reno (like the rest of Nevada, I imagine) is chock full of slot machines, video poker, keno games, and other opportunities to gamble. There were slot machines in the airport terminal. There are video gambling consoles at the bar in many restaurants. They had keno runners in the restaurants, so you didn't have to interrupt your eating in order to gamble. The cost of lodging is relatively cheap, because it is subsidized by the gambling in the casinos.

The casinos felt incredibly depressing to me. They are designed for optimal gambling output, I imagine. They have no windows and the lighting makes it feel like it's the middle of the night, even at noon. You lose all sense of the passing of time.

I am of the opinion that the lottery is a tax on stupidity and desperation. Sure, somebody wins, but the probability of that person being you is lower than the probability that you will get struck by lightning. Similarly, I don't see the appeal of gambling games of chance. Maybe it is because I am not much of a risk-taker. Sure, I take risks, but I mentally calculate the probability of success before I take the jump. And all I have to do is take one look at the opulence of the companies that run casinos to know that gambling is a risk not worth taking!

As I was eating breakfast yesterday, I was offered the "opportunity" to play $1 Keno. In this "game," you pick any three numbers from 1 to 80. The casino draws twenty numbers from that same range, and if your three numbers were all drawn, you can win $35 and a $15 meal voucher.

I did a quick back-of-the-envelope calculation of my odds. Roughly speaking, there is a 1 in 4 chance that any number I pick will be drawn. So the chances of all my numbers being drawn is roughly (1/4)3 = 1/64 ≈ 1.56%. I knew that this calculation was not exact, but it gave me enough information to know that this game was set up in favor of the house.

Every time you play, you pay them a dollar. Once in 64 games, they have to give you $35 plus the meal voucher. If you spent the entire thing, that's still only $50 that they've given out, whereas they've taken in $64, for a net profit of $14.

The exact odds are actually lower. Here's how to compute them:

Let's suppose that you're picking numbers in the range 1-80 and that the casino has already drawn their numbers. The chance that the first number you pick is one that they drew is 20/80, because there are 20 numbers that they drew out of a total of 80. Let's suppose that you got that one right. What are your chances of getting the next one right? Well, there are now 19 possible right numbers out of a total of 79 numbers (because we've picked one of the right numbers and we can't pick it again). So the probability that we got this one right is 19/79. Similarly, if we have been lucky so far and are picking the third number, the probability that we pick another drawn number is 18/78 (because we've picked two of the right numbers and we can't pick them again). So the exact odds are 20/80*19/79*18/78 = 6840/492,960 ≈ 1.39%.

I wrote a little program to illustrate how this keno game works. It's short enough that I'll include my source right here.

#include <stdio.h>
#include <math.h>

int main() {
  long i = 0, ngames = 10000000, nwins = 0, npicked2 = 0, npicked1 = 0;
  int nmatched = 0, ndrawn = 20, nmax = 80, rand0, rand1, rand2;

  for (i = 0; i < ngames; i++) {
    rand0 = rand()%nmax + 1;
    do {
      rand1 = rand()%nmax + 1;
    } while (rand1 == rand0);
    do {
      rand2 = rand()%nmax + 1;
    } while ((rand2 == rand0) || (rand2 == rand1));
    nmatched = ((rand0 <= ndrawn) + (rand1 <= ndrawn) + (rand2 <= ndrawn));
    if (nmatched == 3) {
    } else if (nmatched == 2) {
    } else if (nmatched == 1) {
  printf("Out of %ld games, you won %ld of them, or %f percent\n", ngames, nwins, (100.0*nwins)/ngames);
  printf("You picked exactly two numbers correctly %ld times, or %f percent\n", npicked2, (100.0*npicked2)/ngames);
  printf("You picked exactly one number correctly %ld times, or %f percent\n", npicked1, (100.0*npicked1)/ngames);
  printf("You picked no numbers correctly %ld times, or %f percent\n", (ngames-nwins-npicked2-npicked1), (100.0*(ngames-nwins-npicked2-npicked1))/ngames);
  printf("You paid %ld dollars, and won back %ld dollars\n", ngames, 50*nwins);
  printf("For a profit of %ld dollars (for the casino)\n", ngames - 50*nwins);
  return 0;
And here's the output of my program:

theano:~/misc rebecca$ gcc -o keno keno.c -lm
theano:~/misc rebecca$ ./keno
Out of 10000000 games, you won 139116 of them, or 1.391160 percent
You picked exactly two numbers correctly 1389265 times, or 13.892650 percent
You picked exactly one number correctly 4307469 times, or 43.074690 percent
You picked no numbers correctly 4164150 times, or 41.641500 percent
You paid 10000000 dollars, and won back 6955800 dollars
For a profit of 3044200 dollars (for the casino)

So as you can see, if I play keno ten million times, I pay ten million dollars and earn back nearly $6.96 million (including the meal voucher), resulting in a profit of over $3 million for the casino. So they earn a 30% profit on my "purchase" of keno.

This was the simplest game that they offered, but it is illustrative of all the games in a casino. You can see why I did not play any games of chance.

For keno, they draw actual balls. I wonder what sort of random number generation techniques they use for the video gambling games. I am curious to know, because it might be possible to take advantage of flaws in the random number generation. I've also read about people who used a computer program to defeat roulette. They used their cell phone to gather information about the current velocity and deceleration of the wheel and compute in which quadrant the wheel will stop. They had enough success at it that they made over £1.2 million. Not bad for a night's work!

Without fancy equipment like those three had at their disposal, your chances of overcoming probability are slim at best. My advice: don't bother. Keep your money and spend it on something else.


Scott, professor of physics said...

I thought for a while about the random number generators one might use for electronic gambling. First of all, the algorithms are regulated by law, although I didn't quickly find a source that describes the legal prescriptions.

But upon thinking about it I can imagine a couple of scenarios:

1) hardware-based random number generation: for example, looking at the Josephson noise on a resistor. Actually random, rather than pseudo-random, which would prevent employees from getting hold of the seed values and telling their friends in a computer science department what's coming. If I were a gambling commissioner this is what I would want. The hardware-generated numbers themselves aren't uniformly distributed or anything like that, so maybe they would just be used to generate a random number seed for a second algorithm.

2) A "traditional" linear congruent random number algorithm. If chosen to have large enough modulus, and possibly with a shuffle algorithm attached (see Numerical Recipes ran1, if I recall correctly, although I wouldn't use just ran1 in a casino), this ought to be plenty random and virtually unpredictable from the outside. That is to say, in practice one could never get enough data to determine the seed and crack the routine. Especially given the fact that we don't ever see the full random number output on a slot machine, but just get some coarsely discretized version and don't even know the mapping between random number output and slot outcome.

While even simple versions of these routines would be sufficient to prevent outsiders from gaming the system, the danger with any pseudo-random algorithm is that a hacker on the inside who understood the routines could conceivably find out the seed values and then predict future outcomes. So I really hope that Vegas is using a hardware generator at some point in the process.

EcoGeoFemme said...

But you're buying hope, right?

You sound like me when I walk through a forest. Instead of just seeing the beauty as it is, I see all the processes; water dripping off of a leaf isn't a raindrop, it's throughfall carrying nutrients from so many sources to the soil, which will perolate through, which will diffuse to a root and so on.

Sometimes it's nice to not know. :)

Tony said...

After nearly a full-semester of Java, I actually understand your code now.

Anonymous said...

Depending on which Keno you play, some areas pay for a 2 number match as well as the 3 number match. This increases the chances of winning. If you have time, are you able to workout the percentage of wins for a 2 number permutation match. Say they will pay you $1 back for a 2 number match, this will increase your wins.

If you played all combinations of 80 numbers @ 492,960 games, you have 6,840 chances of matching 3 numbers. How many chances to match 2 numbers? My math came up with 68,400 matches of 2 numbers.

Rebecca said...

Anonymous, if you can win by matching two numbers, the payback for that match will be correspondingly lower. They will make sure that no matter what you do, the house comes out ahead.

According to my math, if you pick only two numbers, you have a 20/80*19/79 = 6.01% chance of winning.

If you pick three and get credit for selecting two out of those three correctly, then you have a 13.9% chance of winning at this level, and a 1.4% chance of winning by selecting all three correctly (according to my simulation). This matches with your math.