2022-09-19 13:48:43 -07:00
|
|
|
#!/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;
|
2022-09-19 13:50:42 -07:00
|
|
|
my @tix;
|
2022-09-19 13:48:43 -07:00
|
|
|
while ($curTix < $tixCount) {
|
2022-09-19 13:50:42 -07:00
|
|
|
$tix[$curTix] = [];
|
|
|
|
while (@{$tix[$curTix]} < $perTix) {
|
2022-09-19 13:48:43 -07:00
|
|
|
my $rr;
|
|
|
|
while (read($randomFH, $rr, 4) != 4) { }
|
|
|
|
$rr = unpack("I", $rr) % scalar(@choices);
|
2022-11-09 11:14:47 -08:00
|
|
|
$rr = $choices[$rr];
|
2022-09-19 13:48:43 -07:00
|
|
|
if (scalar(keys(%done)) < @choices) {
|
|
|
|
next if defined $done{$rr};
|
|
|
|
$done{$rr} = 1;
|
|
|
|
} else {
|
2022-09-19 13:50:42 -07:00
|
|
|
my %inThisTicket = map { $_, 1 } @{$tix[$curTix]}; next if $inThisTicket{$rr};
|
2022-11-09 11:14:47 -08:00
|
|
|
|
|
|
|
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}++;
|
2022-09-19 13:48:43 -07:00
|
|
|
}
|
2022-09-19 13:50:42 -07:00
|
|
|
push(@{$tix[$curTix]}, $rr);
|
2022-09-19 13:48:43 -07:00
|
|
|
}
|
|
|
|
$curTix++;
|
|
|
|
}
|
|
|
|
my $count = 1;
|
2022-09-19 13:50:42 -07:00
|
|
|
foreach my $ticket (@tix) {
|
2022-11-09 11:14:47 -08:00
|
|
|
print sprintf("Ticket %2d: ", $count), map { sprintf("%2d ", $_) } sort { $a <=> $b } @$ticket;
|
|
|
|
print "\n";
|
|
|
|
$count++;
|
2022-09-19 13:48:43 -07:00
|
|
|
}
|
2022-11-09 11:14:47 -08:00
|
|
|
#print "\n\nStats\n";
|
|
|
|
#foreach my $choice (sort { $a <=> $b } keys %done) {
|
|
|
|
# print sprintf("%2d used: %2d\n", $choice, $done{$choice});
|
|
|
|
# }
|