use strict; use warnings; # My solution: sub pickInts { my( $count, $min, $max )= @_; my( @pick, %pick ); while( 0 < $count-- ) { my $i= int( $min + rand($max-$min+1) ); for( $pick{$i} ) { $i= $_ if defined; $_= $min++; } push @pick, $i; } return wantarray ? @pick : \@pick; } # For comparison: #use List::Util qw( shuffle ); # I don't have this module, so I stole its code: sub shuffle (@) { my @a=\(@_); my $n; my $i=@_; map { $n = rand($i--); (${$a[$n]}, $a[$n] = $a[$i])[0]; } @_; } # Sample uses: # Pick 3 integers from the range 1..10: my @list= pickInts( 3, 1, 10 ); print "@list\n"; print join " ", (shuffle(1..10))[0..2], $/; $|= 1; # Pick 10 integers from the range 100000..999999: @list= pickInts( 10, 100000, 999999 ); print "@list\n"; print join " ", (shuffle(100000..999999))[0..9], $/; #### > perl pick.pl 2 1 9 4 10 9 676232 765332 337745 257079 672444 306794 410807 382463 952100 532838 ^C >