my @population = (1..10); # destructive source my $n = 3; # desired selection size my @selection; # result appears here while (@selection < $n) { my $swapper = int rand @population; push @selection, $population[$swapper]; if ($swapper < $#population) { $population[$swapper] = $population[-1]; } pop @population; }
-- Randal L. Schwartz, Perl hacker
Be sure to read my standard disclaimer if this is a reply.
In reply to •Re: Select three random numbers between 1..10
by merlyn
in thread Select three random numbers between 1..10
by Perl_User
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |