small-hacks/megabucks.plx

44 lines
1.1 KiB
Text
Raw Normal View History

#!/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";
}