in reply to Randomize an array
How random do you want it, and what do you mean by "randomize"? I assume you want a random permutation, all permutations equally likey.
The traditional solution is something like:
This does @array swaps, and can be shown (see Knuth) that all permutations are equally likely -- assuming I haven't made any coding mistakesfor $i ($#array..0) { my $j = rand($i); @array[$i,$j] = @array[$j,$i]; }
This is probably the fastest randomization algorithm, although various implementations might be faster than mine.
|
---|
Replies are listed 'Best First'. | |
---|---|
RE: Re: Randomize an array
by merlyn (Sage) on Sep 08, 2000 at 04:58 UTC | |
by BlaisePascal (Monk) on Sep 08, 2000 at 05:12 UTC |
In Section
Seekers of Perl Wisdom