#!/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 Ball, Megaplier ## The Megaplier is ignored open(FILE,"megamillion.csv") or die "Cannot open csv: $!\n"; my @csv = ; 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,$bonus) = 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 number 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();