Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re^2: Randomize elements among several arrays, while maintaining original array sizes.

by Roy Johnson (Monsignor)
on Aug 01, 2008 at 19:07 UTC ( #701760=note: print w/ replies, xml ) Need Help??


in reply to Re: Randomize elements among several arrays, while maintaining original array sizes.
in thread Randomize elements among several arrays, while maintaining original array sizes.

Brilliant!

Note that you can swap without temp variables:

for( 0 .. $#$ref ) { my $p = $_ + rand( $n-- ); @{$ref}[$p,$_] = @{$ref}[$_,$p]; }
You can even use List::Util 'shuffle':
sub my_shuffle2 { my $ref = \@_; use List::Util 'shuffle'; @{$ref}[0..$#$ref] = shuffle(@$ref); return unless defined wantarray; return wantarray ? @{ $ref } : $ref; }

Caution: Contents may have been coded under pressure.


Comment on Re^2: Randomize elements among several arrays, while maintaining original array sizes.
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (12)
As of 2015-07-30 17:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (273 votes), past polls