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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.