Wednesday, April 4, 2007

How to Win the Mega Millions Lottery

I wrote a PERL program that reads a simple text file of all the past winning numbers. You can copy and paste the numbers off the State Lotto Website. Make a text file called numbers.txt with the following format:

02/20/2007 01 09 26 46 51 11
02/13/2007 03 09 24 29 41 41
01/23/2007 03 05 15 26 53 35
01/19/2007 04 28 30 31 35 17
01/16/2007 04 08 15 33 52 10
01/05/2007 02 12 44 46 51 06
12/29/2006 03 04 10 39 50 29

While you're creating your history file, it's OK if you don't get the dates in order or if there's a lot of duplicates, the program will cull them out! Now run the PERL script. You will get a list of the most common winning numbers.

This works on the logic that if they've won before; they'll win again! If you like, you can play the least commonly drawn numbers as they are probably "due" to be drawn.

#!/usr/bin/perl
#read list of winners
open FILE, "numbers.txt" or die "Couldn't open winners";

#push unique drawing dates into array
while () {
$_ =~ s/\D/ /g;
($month, $day, $year, $a, $b, $c, $d, $e, $power) = split;
$drawing = $year . $month . $day . " " . $a ." ".$b." ".$c." ".$d." ".$e." ".$power."\n";
push (@uniq, $drawing) unless $seen{$drawing}++;
}

#count how freq each power ball number is drawn
foreach $drawing (@uniq) {
($date, $a, $b, $c, $d, $e, $power) = split / / , $drawing;
$ball{$a}++;
$ball{$b}++;
$ball{$c}++;
$ball{$d}++;
$ball{$e}++;
$pb{$power}++;
}

#Sort by most common winners
foreach $value (sort {$ball{$a} <=> $ball{$b} } keys %ball)
{
print "$value $ball{$value}\n";
}

#Sort and find the most winningest Power Ball
foreach $value (sort {$pb{$a} <=> $pb{$b} } keys %pb)
{
print "$value $pb{$value}\n";
}

That's it! There's some hooks that aneed development if you like. The $date is parsed out so you can use it to see when a number was last drawn. Also, the powerballs don't print out too neatly.

Comments are welcome. Good luck!