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!
Caution: Contents may have been coded under pressure.
Note that you can swap without temp variables:
You can even use List::Util 'shuffle':for( 0 .. $#$ref ) { my $p = $_ + rand( $n-- ); @{$ref}[$p,$_] = @{$ref}[$_,$p]; }
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.
In Section
Seekers of Perl Wisdom