From 05b86946fa6b94972400e5ca4e4e5d3726d6b9ca Mon Sep 17 00:00:00 2001 From: "Bradley M. Kuhn" Date: Mon, 19 Sep 2022 13:48:43 -0700 Subject: [PATCH] Program to pick random Oregon megabucks tickets without overlap --- megabucks.plx | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 megabucks.plx diff --git a/megabucks.plx b/megabucks.plx new file mode 100644 index 0000000..8775257 --- /dev/null +++ b/megabucks.plx @@ -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 \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"; +}