This uses a partial Fisher-Yates shuffle to efficiently pick a random selection of elements from a contiguous range of integers, using a hash to prevent using a lot of memory when the range of integers to choose from is huge.
If the number of integers you want to select is nearly as large as the range you are selecting from, then using a hash in this way isn't a big win and you are probably better off just putting the range of integers into an array and doing a partial Fisher-Yates shuffle on it.
If you can rely on your version(s) of Perl having the // operator, then the code can be simplified, replacing all but the first and last two lines of the loop with the two commented-out lines.
Command-line usage of included sample code:
perl pickFromRange.pl [ count [ max | min max ] ] pickFromRange [ count [ max | min max ] ]
Count defaults to 20. If max is not given, then the range 100..999 is used. If max is given but not min, then 1..max is used.
(Minor updates applied.)
#!/usr/bin/perl -w use strict; sub pickFromRange { my( $count, $min, $max )= @_; my $range= $max - $min + 1; die "pickFromRange: $max - $min + 1 < $count" if $range < $coun +t; my @pick; my %replace; while( 0 < $count-- ) { my $pick= $min + int( rand($range) ); my $next= $replace{$pick}; push @pick, defined($next) ? $next : $pick; # push @pick, $replace{$pick} // $pick; $next = $replace{$max}; $replace{$pick}= defined($next) ? $next : $max; # $replace{$pick}= $replace{$max} // $max; delete $replace{$max--}; $range--; } return @pick; } my $count= @ARGV ? shift(@ARGV) : 20; my( $min, $max )= !@ARGV ? (100,999) : 1==@ARGV ? (1,@ARGV) : @ARGV; my @pick= pickFromRange( $count, $min, $max ); print "@pick\n";
In reply to pickFromRange by tye
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |