in reply to How to generate distinct random numbers?

This should probably have some error checking to make sure that $start is less than $end, and that $end-$start >= $num. If your intent is that $end shoudl be inclusive it will need small tweak also.

#! perl -slw use strict; sub distinct_random_int{ my($num, $start,$end) = @_; my %uniq; $uniq{ $start + int( rand $end-$start ) } = undef while keys %uniq < $num; return keys %uniq; } print for distinct_random_int 10, 20, 30; __END__ P:\test>388533 27 25 28 21 26 20 22 24 23 29

That works, but if $num is close to being the same size as the range, then you would be better to shuffle the range and then select the first $num elements:

#! perl -slw use strict; use List::Util qw[ shuffle ]; sub distinct_random_int{ my($num, $start,$end) = @_; return ( shuffle $start .. $end )[ 1 .. $num ]; } print for distinct_random_int 10, 20, 30;

Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"Think for yourself!" - Abigail
"Memory, processor, disk in that order on the hardware side. Algorithm, algorithm, algorithm on the code side." - tachyon