Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Random Sampling

by Anonymous Monk
on Jun 25, 2002 at 20:22 UTC ( #177201=note: print w/ replies, xml ) Need Help??


in reply to Random Sampling

Your selection_sample routine is not correctly coded. The idea is to randomly select a sample of n elements as you go, there should not be duplicates in the sample. Change your while loop to the following for a more faithful representation of Knuth's algorithm.

while ( @results < $num ) { if ( (@$array - $pos) * rand() < $num - @results ) { push @results, $array->[$pos]; } $pos++; }


Comment on Re: Random Sampling
Download Code
Re: Re: Random Sampling
by demerphq (Chancellor) on Jun 26, 2002 at 17:02 UTC
    Hmm, while you are correct that my code was incorrect, I went with a different fix. My fix was simply to change the line
    push @results, $array->[$pos];
    to
    push @results, $array->[$pos++];
    But thanks anyway. /me should have tested once or twice more. *sigh*

    Yves / DeMerphq
    ---
    Writing a good benchmark isnt as easy as it might look.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://177201]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (14)
As of 2014-07-31 19:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (251 votes), past polls