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

My gosh! All those replies with different approaches, and nobody (so far) seems to have tried using a hash <update> kvale's was the only one to use a hash -- it was so short, I missed it, and wrote it again myself </update>:
#!/usr/bin/perl use strict; my %chosen; # you don't need a 10-element array to draw from while ( keys %chosen < 3 ) { my $pick = int( rand(10)) + 1; $chosen{$pick} = undef; } print join " ", keys %chosen, "\n"; # (you could "sort {$a<=>$b) keys %chosen", if that matters)
This sort of problem does not merit a lot of optimization, but I think the extra iterations that might be needed on occasion to avoid repeat picks would, on balance, take less time than various amounts of array splicing, shuffling, etc...