Lottery Simulator

Back in the first programming course I took at the university level (intro to java) I got to thinking about the lottery. I ended up writing a simple program that did the following:

  1. Read in 6 numbers provided by the user (between 1 and 49, as per the lottery in my province).
  2. Have the program pick 6 numbers at random and compare them to my pick.
  3. Loop until the program matches my pick and return the number of loops.

I didn’t have a point in the exercise, I just wanted to see if I could do it. At that point it was one of the more complicated things I had accomplished and I’d done it for myself, not for grades (which made it more awarding).

I’ve lost the source code for that program, but I’d recently become interested in that simulation, again. I’ve rewritten it in C and made it scalable with the pool of numbers to pick from (ie, 49) as well as the quantity of numbers to pick (ie, 6).

I’m interested in finding the average number of games to play, chance of winning a lottery based on pool size and pick size. In the standard lottery in Canada the pool size is 49 and the player picks 6 numbers. It’s a suckers’ tax. I’ll let you in on a secret. So far, the average number of games needed to play in order to win that lottery is 13.5 million games. Like, for example, when Coke tells you that you have a 1 in 6 chance of winning another bottle of Coke I am telling you that you have a 1 in 13,500,000 chance of winning the lottery. (edit: I’m aware that this is accomplished with the function n choose k that we learn in calculus. Googling 49 choose 6 gives the result 13,983,816)

I’ll get more into that later. Expect a “Science” page to open up here in the next little while with a portal to the simulation. So far I’ve written the code to run the stats, process them into mean, minimum and maximum values, and to plot the data and save them as nice images. The program also compiles the results into it’s own html page for easy viewing. About all that’s left now is to have the program index the pages and link them together for easier navigation.

And of course, the source code will be made available to all.

Leave a Reply


Staypressed theme by Themocracy