[Chicago-talk] How perl will help me win the Lottery

Jim Thomason jthomasoniii at yahoo.com
Fri Feb 20 10:53:39 CST 2004


Unless you've been living under a rock, you're aware
of the fact that the multi-state Mega Millions lottery
game hasn't been won lately, pushing the jackpot up to
an estimated $220 million. The drawing is tonight at
10 o'clock on WGN Channel 9.

Now, of course, the lottery is an utterly random
event. There is no way to predict its outcome, no way
to guess what it'll be, no system to play that will
work. It's purely random.

Nonetheless, people like to have control over things,
even if it's only perceived control. If you just get a
random quick pick and lose, you may sit back and
wonder if you really did all you could to win. Maybe
you should have played your favorite pet's birthday or
your lucky number. Maybe it would have made a
difference.

Fortunately, perl can help assuage that future guilt.
The lottery people are helpful enough to provide us
with a list of past lotto winners for the mega
millions game, available at: http://tinyurl.com/2pkxd

But that's a lot of data to look through. Sure, the
22nd is my birthday, and I see a lot of 22s in there,
but how many are there really? Well, never fear. Perl
can help us immensely. The mega millions data cruncher
is attached.

Basically, it'll iterate through your data file and
enumerate out the most popular numbers chosen (5 or
47, chosen 84 times each); the most chosen 2 digit
combo (1 & 22 (score!), 16 times) and so on. Now, how
you use that data is left as an exercise for the
reader. I included a few example tickets to build. For
example, the ticket constructed out of the most
frequently chosen numbers is 4, 5, 26, 32, 47 with a
megaball of 3. But acquiring the data is easy,
figuring out how you want to combine it together and
use it is the tricky part.

Naturally, this is all purely for entertainment
purposes and I really don't care to have someone start
emailing me saying that all of the analysis in the
world will have no bearing at all on the outcome.

Still. People do like having control over things.

I'll expect a 10% commission if anybody wins big off
of this. ;)

-Jim......

__________________________________
Do you Yahoo!?
Yahoo! Mail SpamGuard - Read only the mail you want.
http://antispam.yahoo.com/tools
-------------- next part --------------
use strict;
use warnings;

#okay, we're going to need to figure out all the combinations of a given set of white balls.
#that way we can do analysis on how many times a ball appeared, appeared with 1 other ball, 2 other balls, etc
#
#The function is recursive. If we're choosing one, then return a list of all digits. If we're choosing more than
#one, then iterate through the digits we have and choose (n - 1) on the remainder. Iterate through what's returned
#and tack on the digit we have to get the current combination.
sub combine {
  my $choose = shift;
  my @digits = @_ or return ();
  
  if ($choose == 1) {
    return map {[$_]} @digits;
  } else {
    my @return = ();
    foreach my $idx (0..$#digits) {
      my @combos = combine($choose - 1, @digits[$idx + 1 .. $#digits]);
      foreach my $combo (@combos) {
        push @return, [$digits[$idx], @$combo];
      };
    };
    return @return;
  }
};

#set up some space to store the combinations choose 1, choose 2, choose 3, etc.
my $combinations = [undef, {}, {}, {}, {}, {}];

#set up a hash table to store the megaballs
my %megaballs = ();

while (<DATA>) {
  
  #parse out the whiteballs and the megaball
  my @whiteballs = /(\d+),(\d+),(\d+),(\d+),(\d+).+Mega Ball:\s*(\d+)/g;
  my $megaball = pop @whiteballs;
  
  #sort the whiteballs, just to be clean (makes the output easier to read)
  @whiteballs = sort {$a <=> $b} @whiteballs;
  
  #iterate through the combinations (choose 1, choose 2, etc.)
  foreach my $combination (1.. at whiteballs) {
    #grab the combos
    my @combos = combine($combination, @whiteballs);
    
    #and for each of those, join the arrayref into a string and iterate its count
    foreach my $combo (@combos) {
      $combo = join(',', @$combo);

      $combinations->[$combination]->{$combo}++;
    };
  };

  $megaballs{$megaball}++;

};

#Done! Now start building sample tickets.

#for example, build a lottery ticket made out of the top 5 selected numbers + the top selected mega ball

#grab the white balls for our choose 1 set, that is the balls that appeared most individually
my $whiteballs = $combinations->[1];

my @top5white = sort {$a <=> $b} (sort {$whiteballs->{$b} <=> $whiteballs->{$a} || $a cmp $b} keys %$whiteballs)[0..4];
my ($mega1, $mega2, $mega3) = (sort {$megaballs{$b} <=> $megaballs{$a} || $a cmp $b} keys %megaballs)[0,1,2];

print "Most likely combo is White balls: @top5white Megaball: $mega1\n";

#of course, we shouldn't put all our eggs in one basket. Let's also build a ticket with the #2 mega ball.
print "Most likely combo is White balls: @top5white #2 Megaball: $mega2\n";

#for example, build a lottery ticket made out of the top selected 2 ball combo + the top selected 3 ball combo
# + the top selected mega ball

my $whiteballsChoose2 = $combinations->[2];
my $whiteballsChoose3 = $combinations->[3];

#technically, there could be an overlap here, since the most chosen 2 ball combo could share a digit with the most
#chosen 3 ball combo, but in this case, I know that they don't.
my $top5whiteChoose2 = (sort {$whiteballsChoose2->{$b} <=> $whiteballsChoose2->{$a} || $a cmp $b} keys %$whiteballsChoose2)[0];
my $top5whiteChoose3 = (sort {$whiteballsChoose3->{$b} <=> $whiteballsChoose3->{$a} || $a cmp $b} keys %$whiteballsChoose3)[0];

print "Most likely 2 + 3 is White balls : $top5whiteChoose2,$top5whiteChoose3 Megaball: $mega1\n";

#okay, let's also build a ticket with the most chosen 4 ball combo + the top chosen ball + the #3 mega ball
#(same overlap could exist between the 4 ball combo and the #1 ball, so be careful)

my $whiteballsChoose4 = $combinations->[4];

my $top5whiteChoose4 = (sort {$whiteballsChoose4->{$b} <=> $whiteballsChoose4->{$a} || $a cmp $b} keys %$whiteballsChoose4)[0];

print "Most likely 4 + 1 is White balls : $top5whiteChoose4,$top5white[0] #3 Megaball: $mega3\n";

__DATA__

#insert your mega millions data here, download from
#http://www.megamillions.com/winningpicks/download_numbers_execute.asp
9-6-1996; : 5,11,29,47,50; Mega Ball: 17
9-13-1996; 3,4,9,30,47; Mega Ball: 1
.
.
.


More information about the Chicago-talk mailing list