Random number challenge

James Lance james at thelances.net
Fri Jun 13 10:21:25 MDT 2008


So this fails the easy to remember requirement, but I coded this up at a 
previous PLUG meeting so I though I would included it.

#!/usr/bin/perl
use strict;

my $limit = $ARGV[0];
unless ( $limit ) { die "Please specify a limit ex: ./grab_bag_generator.pl 
25\n";}

my %seen = ();

my $run = 1;

while ($run) {
    my $num = int(rand($limit)) + 1;
    unless ( $seen{$num} ) {
        print "$num\n";
        $seen{$num}++;
        print "Run again? (yn) ";
        $_ = <STDIN>;
        chomp;

        if ( $_ =~ m/[nN]/ ) {
            $run = 0;
        }
    }

    if ( scalar(keys %seen) >= ($limit) ) { $run = 0 }
}

-James




More information about the PLUG mailing list