in reply to Select three random numbers between 1..10

In my experience, the circumstances for random number selection are relevent to the process. Many selection algorithms are costly in terms of time or computing power or are unnecessarily complex for the task at hand. Selecting 10 random numbers may not require the same complexity as selecting 10,000. Here are two possibilities I've used before.
#!/usr/perl use warnings; use strict; my @tmpary = (); my @num = (); my $ulmt = 10; my $nrneeded = 3; srand; # if the order of selection of random number is not important my $idx = 0; my $cntr = 0; # this does waste the 1st element but simplifies indexing # and does cost the memory but for small lists this isn't # significant push @tmpary, undef for (0..$ulmt); while($cntr < $nrneeded){ $idx = int(rand($ulmt-1)+1); # map range into (1..ulmt) unless($tmpary[$idx]){ $cntr++; $tmpary[$idx] = 1; } } $tmpary[$_] && push @num, $_ for (0..$ulmt); print "$_, " foreach (@num); print "\n"; # if order of selection of random numbers is important my %idx = (); my $val = 0; @num = (); while(scalar keys %idx < $nrneeded){ $val = int(rand($ulmt-1)+1); # map range to (1..ulmt) $idx{$val} ||= 1 && push @num, $val; } print "$_, " foreach (@num); print "\n"; exit;