Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

RE: Randomize an array

by KM (Priest)
on Sep 08, 2000 at 00:13 UTC ( #31474=note: print w/replies, xml ) Need Help??


in reply to Randomize an array

Fisher-Yates shuffle.

my @array = qw /one two three four five six/; for (my $i = @array; -- $i;) { my $r = int rand (1 + $i); @array [$i, $r] = @array [$r, $i] unless $r == $i; }

Or use Algorithm::Numerical::Shuffle which implements this for you

use Algorithm::Numerical::Shuffle qw/shuffle/; my @array = shuffle qw/one two three four five/;

Cheers,
KM

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2019-05-24 06:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you enjoy 3D movies?



    Results (147 votes). Check out past polls.

    Notices?
    • (Sep 10, 2018 at 22:53 UTC) Welcome new users!