Posts tagged: lottery

Lotto, detailed.

The lotto page has been updated. The focus has been maintained; the point was to examine the common Lotto 6-49.

Taking for example a hypothetical Lotto 4-15, we select 4 numbers of our choosing from the available numbers ranging between 1 and 15. The 4 you pick are matched against another 4 random numbers drawn from the same range of numbers, 1 through 15, at a later date. You all know how a lottery works.

The point being, you want your 4 numbers to match the 4 numbers later drawn at random. But what are your chances of winning with 4 numbers picked from a pool of 15? Google says 15 choose 4 equals 1365. What’s that 15 choose 4 thing? It’s a binomial coefficient (n choose k), but I won’t get into too much detail about that here. Suffice it to say, it calculates the number of combinations to be made when choosing k numbers from a pool of size n.

So with 4 numbers from a total 15 you have a 1 in 1365 chance of having your 4 numbers picked again.

We can simulate that. The source code is right here. It’s the lotto sim. I set it to pick 4 numbers of it’s choosing from 15 and hold on to them. I also told it to simulate and record stats for 1000 wins.

So the sim chooses the 4 numbers it’s going to play then it picks 4 more, like a lotto-drawing would. If it matches, it’s happy and stops playing. If it doesn’t match then it plays the 4 numbers it originally chose against a new drawing of 4 (think of it like next week’s drawing). It keeps going until it finds a match and when it does it stops and records the number of games played until it won. Then it picks another 4 and plays until it wins… (based on number of wins you told it to play to).

Here’s a spiffy graph showing the Lotto 4-15 with statistics for 1000 wins.

1000 wins gives us an “ok” real-world result for the lotto 4-15. We see that the average starts to converge on the odds of winning this type of lottery. Remember 15 choose 4 gave us the value 1365 and the results here are showing a value of 1380.69 which is reasonable.

Lotto 5-14 average plays until win

Click the Science tab at the top of this page and then the Lotto section. Visit the science page in the wiki. There you can find more information along with results for the common Lotto 6-49.

An important lesson in saving…

…and backing up your saves in case you accidentally delete the copy you are working with.

Let this be a lesson to me. I was working tonight on the lottery simulation. I wrote some more code introducing new plotted data. All was well.

I thought maybe I’d take a break. I loaded up “20.000 Light Years in Space” and played a couple rounds of the beginner level.

I figured I’d had enough fun, it was now time to package up my hard work and post to the science page. I thought to myself, there’s no sense in tarring up all the results files, there’s a couple hundred of those and they are taking up way too much space!

Lesson: The ‘rm’ command is unforgiving.

I “permanently” deleted some of the new code I had written.

I know I could dip into the computer forensics field and recover my lost file, but it’s only 30 minutes of code I lost. 30 minutes is still an inconvenience. So be warned! Make sure you are rm`ing what you mean to rm.

I’m re-writing it now and it should be posted soon. I’ll update this post when it is.


I’ve fixed my mistake, the new code is better than it was before I erased it all. And now there are 979 new and updated files in the Lotto section. Perl really makes it easy to generate stuff. 979 files by hand? I wouldn’t bother.

Winning Numbers

The lottery simulation is in a very usable state. I’d just like to make a note before I forget, it was written and implemented in Linux. I use Ubuntu 7.10 (and am very excited at the release of Ubuntu 8.04 in two days). The required programs, should you choose to download and play with the code, are as follows:

  1. Scilab for crunching the data (ie, calculating averages).
  2. Gnuplot for plotting the data and saving them as images (where Scilab falls short).

You may also need to “sudo apt-get install build-essential” if you want to alter and compile the source, particularly if you’re using Ubuntu. For some reason it wasn’t included with the default Ubuntu 7.10 install.

I’m continuing to generate data and eventually (it takes a long time) I’ll have 10000 to 50000 points of data for every combination of n choose k between 2 to 49.

Coming up next…

I’m playing with sorting algorithms. First up, bubble sort. So keep an eye out on the science page.

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.

Staypressed theme by Themocracy