Program to pick random Oregon megabucks tickets without overlap
This commit is contained in:
parent
21d222a49d
commit
05b86946fa
1 changed files with 43 additions and 0 deletions
43
megabucks.plx
Normal file
43
megabucks.plx
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
#!/usr/bin/perl
|
||||
# megabucks.plx -*- Perl -*-
|
||||
|
||||
use strict;
|
||||
use warnings;
|
||||
|
||||
use List::Util qw(min);
|
||||
|
||||
open(my $randomFH, "/dev/random") || die "You have no /dev/random!";
|
||||
|
||||
if (@ARGV != 4) {
|
||||
print "usage: $0 <TICKET_COUNT> <CHOICES_PER_TICKET> <MIN> <MAX>\n";
|
||||
exit 1;
|
||||
}
|
||||
|
||||
my($tixCount, $perTix, $min, $max) = @ARGV;
|
||||
|
||||
my(@choices) = $min .. $max;
|
||||
my %done;
|
||||
|
||||
my $curTix = 0;
|
||||
my $tix;
|
||||
while ($curTix < $tixCount) {
|
||||
while (@{$tix->[$curTix]} < $perTix) {
|
||||
my $rr;
|
||||
while (read($randomFH, $rr, 4) != 4) { }
|
||||
$rr = unpack("I", $rr) % scalar(@choices);
|
||||
if (scalar(keys(%done)) < @choices) {
|
||||
next if defined $done{$rr};
|
||||
$done{$rr} = 1;
|
||||
} else {
|
||||
next if $rr < 31;
|
||||
my %inThisTicket = map { $_, 1 } @{$tix->[$curTix]}; next if $inThisTicket{$rr};
|
||||
my $min = min(grep { $_ > 31} values %done); next if $done{$rr} > $min;
|
||||
}
|
||||
push(@{$tix->[$curTix++]}, $rr);
|
||||
}
|
||||
$curTix++;
|
||||
}
|
||||
my $count = 1;
|
||||
foreach my $ticket (@{$tix}) {
|
||||
print "Ticket $count: ", join(" ", sort { $a <=> $b } @$ticket), "\n";
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue