Category: | Fun Stuff |
Author/Contact Info | OverlordQ - PM Node: 236515 |
Description: | Takes the results of past lottery drawings and feeds it into a weighted random number generator to guess future results. Horribly inefficient I suspect. |
#!/usr/bin/perl -w use strict; use List::Util qw(shuffle); ## CSV file has following format: ## Game Name, Month, Day, Year, Num1, Num2, Num3, Num4, Num5, Mega Bal +l, Megaplier ## The Megaplier is ignored open(FILE,"megamillion.csv") or die "Cannot open csv: $!\n"; my @csv = <FILE>; close(FILE); ## Initialize the temporary hashes my %hash; my %bonus; ## Inefficient loop to get number counts foreach my $line (@csv) { my ($game,$month,$day,$year,$one,$two,$three,$four,$five,$bonu +s) = split(/,/,$line); $hash{$one}++; $hash{$two}++; $hash{$three}++; $hash{$four}++; $hash{$five}++; $bonus{$bonus}++; } ## Initialize the weighted arrays my @draw; my @mega; ## Loop and push the numbers into the arrays, and shuffle foreach my $key (sort keys %hash) { for(my $count = 0; $count < $hash{$key}; $count++) { push(@draw,$key); } } @draw = shuffle(@draw); foreach my $key (sort keys %bonus) { for(my $count = 0; $count< $hash{$key}; $count++) { push(@mega,$key); } } @mega = shuffle(@mega); sub draw_regular { $draw[ rand(@draw) ]; } sub draw_mega { $mega[ rand(@mega) ]; } ## Keep track of number's we've picked my @picked; ## Loop and draw 5 numbers, pick another incase we draw the same numbe +r twice. for(my $i=0;$i<=4;$i++) { my $number = draw_regular(); foreach my $pick (@picked) { while($number == $pick) { $number = draw_regular(); } } push(@picked,$number); } ## Output the Numbers we drew. print "Draw: " . join(',', sort(@picked)) . "\n"; print "Bonus: " . draw_mega(); |
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Lottery Numbers
by jdporter (Paladin) on May 03, 2004 at 01:36 UTC | |
by OverlordQ (Hermit) on May 05, 2004 at 00:20 UTC | |
by Anonymous Monk on Jun 02, 2015 at 19:31 UTC | |
Re: Lottery Numbers
by TomDLux (Vicar) on May 04, 2004 at 00:22 UTC |