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.

Presumably, his example a, b, and c arrays are datasets from some outside source, so your a+b suggestion likely doesn't add anything. Additionally, I don't even know what you meant to do with @where_to, as you're just assigning strings 'a', 'b' and 'c'.

The simplest way to do what the OP wanted would be as pjotrik suggested. Your recommendation of List::Util's shuffle is definitely a good suggestion though.

Addendum: A quick and dirty example...

#!/usr/bin/perl -w use strict; use List::Util 'shuffle'; my @array_a = (10, 22, 34, 21, 20, 12, 32, 31, 91, 20); my @array_b = (10, 22, 34, 21, 20, 12, 32, 31, 91, 20, 11, 32, 44, 51, + 10, 22, 82, 71, 61, 54); my @array_c = (14, 25, 64, 71, 80); mass_shuffle(\@array_a, \@array_b, \@array_c); sub mass_shuffle { # all sub-arrays passed by ref my @arrays = @_; # combine into one large array and shuffle it my @shuffler; foreach my $sub (@arrays) { push @shuffler, @{$sub}; } @shuffler = shuffle(@shuffler); # splice properly sized randomized replacements foreach my $sub (@arrays) { @{$sub} = splice @shuffler, 0, scalar(@{$sub}); } }
  • Comment on Re^2: Randomize elements among several arrays, while maintaining original array sizes.
  • Select or Download Code

Replies are listed 'Best First'.
Re^3: Randomize elements among several arrays, while maintaining original array sizes.
by BioNrd (Monk) on Aug 01, 2008 at 16:50 UTC
    The @where_to, is a list of places where the items are destined to go. I want to not simply shuffle the arrays. That is easy as has been pointed out, and I am aware of how to do this (having learned "some basic perl").

    I want to move the items around between the arrays, while maintaining the array sizes. So items in A can move to A, B, or C.

    It is not so much shuffling the order of each array as it is shuffling where each elements goes and creating new arrays of size A B and C that now have elements drawn from A B or C. In essence, I am trying to set up a permutation test, but having great difficulty with this.

    ---- Even a blind squirrel finds a nut sometimes.