in reply to •Re: Select three random numbers between 1..10
in thread Select three random numbers between 1..10

Urgle. While you pick a fast algorithm, your implementation is not efficient. There's no need to build @selection element by element, while popping off one element of of @population at the time. Just run $n iterations of Fisher-Yates loop, and return the last $n elements of the array. Furthermore,
if ($swapper < $#population) { $population[$swapper] = $population[-1]; }
is less efficient than
$population[$swapper] = $population[-1];
Sure, the if statement prevents a needless assignment, but the expected number of needless assignments is
Σi=0$n 1/(@population - i)
which, while unbounded, grows very very slowly if the array to sample grows. OTOH, the number of compares done is linear with the size of the sample taken.

Abigail

Replies are listed 'Best First'.
•Re: Re: Select three random numbers between 1..10
by merlyn (Sage) on Mar 17, 2004 at 02:19 UTC
    Oddly enough, Abi, I was hoping you'd point out the error of my ways. You have a brilliance that I can only aspire towards. I'm just a street-trained programmer of average class.

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.