# In praise of lotteries

I’ve always wanted to be quotable.

With all the furor about the Powerball lottery in the United States this past week, I have the perfect opportunity.

So, here goes:

“You can’t be functionally literate in the 21st century until you understand the mathematics and probability behind your chances of winning the lottery and be able to write a computer program to simulate your chances of winning.”

I know; it doesn’t work for me either but feel free to quote me anyway.

All the “lottery in the news” is perfect if you teach coding.

In the news, you’ll see reports about how the probability of getting hit three times in a rowboat on Lake Erie is not as high as your chances of winning the lottery.  We just accept it as fact because some speaking head on the television said it.  They must be smart, right?  Then you realize that they’re just reading some other person’s words on their teleprompter.  Those people did their research on the internet probably from some university research project involving huge databases and probability.

But teaching about this is one of the fun moments as a computer science teacher.  I can still hear the complaints – “Siiiirrr, this isn’t math class”.

Yet, in some languages where a random number is actually a number with 7 decimal places and found between 0 and 1, a great deal of mathematics was needed to first of all explain how the computer generated that number and then was forced to do some computations to generate an integer (or as too many students would say “interger”) between 1 and the desired high end.  You might end up with a statement like this:

roll := int(rnd(0)*6)+1

The sheer beauty of the logic and mathematics behind that isn’t always appreciated.

Modern languages make it easy.  You just have a statement like:

roll = math.GetRandomNumber(6)

It was so much more elegant when the statement was cryptic. But the end result was a random number.  Or is it?  Time for a discussion about pseudo-random numbers.

This past week, a few of us computery sciencey people were having fun with the concept in a Facebook discussion.  We convinced Alfred Thompson to do something in his secondary school classes and, as a good social media person, he buckled under the pressure and did it.  And, of course blogged about it. “Lottery Inspired Projects“.

Now, doing something like this typically isn’t done cold turkey.  Like anything you build on a concept.  I actually enjoyed doing this sort of thing with students.  We would start with something simple like flipping a coin a number of times and might end up with a program like you see below in Small Basic.

Simple enough, right?

Next step and, while the above would be developed as a class, the task would be to modify the program to simulate the roll of a six-sided die.  And, of course, one die isn’t enough for most games so we’d then modify the new program to have two dice.  Inevitably, we’d sit back and wonder – just how fair is the computer in doing this?  We’d actually do a hands-on activity after I visited the mathematics department to borrow their class sets of dice.  We’d roll the dice and tally the results to get an idea of what combinations were more likely.  Then, a little plotting on graph paper was in order to get a visual of what it might look like.  Then, back to the computers to write a program that would generate the graph.  By now, there wasn’t a disengaged student in the house.  My Dungeons and Dragons gang were then off on a tangent because there are more things in and out of this world than six-sided dice.

Next, on to the biggy.  The lottery.

When I mention that this was a big step, I would inevitably get one of those “D’uh sir.  It’s just like a dice with 69 sides”.

Flipping a coin or rolling a die is pretty easy stuff.  After all, who cares if you get two heads in a row or if you roll three fours in a roll.

It’s a whole different game when you play the lottery.  Once a ball has been chosen, it gets put into a rack and photographed for the television viewing audience.  You never see the operator pick it up and put it back into the tumbling ball cage.  It’s a concept called “Sampling Without Replacement”.  The concept led to a couple of interesting things.

1. What’s the difference between “Sampling Without Replacement” and “Sampling With Replacement”?  The difference between flipping a coin and simulating a lottery is the absolutely perfect way to teach the concepts
2. Now the biggy – how would you ever write a program that simulates “Sampling Without Replacement”?

At this point in time, the relatively simple concept of flipping a coin is just a fond memory.  We’re now into some pretty sophisticated computational thinking for Grade 11 secondary school students.  To their credit, I can remember classes that wanted me to just stop.  They would take it from there. I always kept a cupboard of computer magazines and some students had remembered typing a game from the magazine into the computer.  And, good games always have a random concept to them.

Noise was never an issue in the classroom.  As long as the talk was on topic, things were good.  I was big on brainstorming or, as one student called it, “thinking out loud”.  I wish I had access to one of today’s phone apps that monitor sounds.  In the beginning, it would be loud but once they got onto something, it died.  I’d love to map that to determine whether they were focused on a solution once they found it or if they didn’t want to share their work with others.

I can remember original solutions that involved less than elegant solutions involving arrays, lists, multiple variables, … Given enough time, most solutions actually worked.  It was fun watching the students work in groups on the project, hiding their work from classmates, and then showing off their logic at the end.

Next steps?  Dealing cards was always fun.  Now you have numbers, letters, and suits.  The fun never ends!

Where would we have been without a lottery as a model?