develooper Front page | perl.beginners | Postings from April 2002

Re: Scripts picks random elements from array , but it repeats som etimes

From:
drieux
Date:
April 29, 2002 13:51
Subject:
Re: Scripts picks random elements from array , but it repeats som etimes
Message ID:
BE1A3426-5BB2-11D6-B775-0030654D3CAE@wetware.com

On Monday, April 29, 2002, at 01:03 , Bob Showalter wrote:

> May I suggest a modification of the algorithm that avoids this
> problem:
>
>    1. generate a random index from 1 to (number of remaining elements)
>    2. use the element at that index
>    3. move the last element into the index generated
>    4. decrement the number of remaining elements.

My complements on a well written analysis of the core algorithm.

My concern of course is that since this strategy is destructive
of the @array being searched, that one might find it slightly
faster to simply slice the array in flight....

http://www.wetware.com/drieux/CS/lang/Perl/Beginners/BenchMarks/randTest.txt



ciao
drieux

---




nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About