small-hacks/megabucks.plx

61 lines
1.5 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) {
$tix[$curTix] = [];
while (@{$tix[$curTix]} < $perTix) {
my $rr;
while (read($randomFH, $rr, 4) != 4) { }
$rr = unpack("I", $rr) % scalar(@choices);
$rr = $choices[$rr];
if (scalar(keys(%done)) < @choices) {
next if defined $done{$rr};
$done{$rr} = 1;
} else {
my %inThisTicket = map { $_, 1 } @{$tix[$curTix]}; next if $inThisTicket{$rr};
my $hundred;
while (read($randomFH, $hundred, 4) != 4) { }
$hundred = unpack("I", $hundred) % 100;
next if ($rr <= 31) and ($hundred > 40);
next if ($rr == 7) and ($hundred > 5);
my $min = min(@done{grep { $_ > 31} keys %done});
next if $done{$rr} > $min;
$done{$rr}++;
}
push(@{$tix[$curTix]}, $rr);
}
$curTix++;
}
my $count = 1;
foreach my $ticket (@tix) {
print sprintf("Ticket %2d: ", $count), map { sprintf("%2d ", $_) } sort { $a <=> $b } @$ticket;
print "\n";
$count++;
}
#print "\n\nStats\n";
#foreach my $choice (sort { $a <=> $b } keys %done) {
# print sprintf("%2d used: %2d\n", $choice, $done{$choice});
# }