in reply to Select three random numbers between 1..10
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Select three random numbers between 1..10
by Abigail-II (Bishop) on Mar 16, 2004 at 23:29 UTC | |
by merlyn (Sage) on Mar 17, 2004 at 02:19 UTC | |
|
Re: •Re: Select three random numbers between 1..10
by mstone (Deacon) on Mar 18, 2004 at 03:18 UTC |