g0n has asked for the wisdom of the Perl Monks concerning the following question:

Fellow monks,

I have an array in the following format:

my @dataArray = (\@inputValue1,\@outputValue1,\@inputValue2,\@outputVa +lue2..)

to the tune of 1700 pairs of input/output pairs (read in from a file). Unfortunately, these data are ordered in the file, whereas I want them random. This code:

# Subroutine to jumble the data sub jumble { for (1..50) { my $endpoint = int(rand(scalar @dataArray -1)); if ($endpoint % 2){$endpoint++} my (@tempArray) = (@dataArray[$endpoint..(scalar @data +Array-1)]); push @tempArray,(@dataArray[0..$endpoint-1]); @dataArray = @tempArray; } }

does the job of randomizing them well enough, but is there a better way?

TIA.

Update: In case I haven't made it clear, \@inputValue1 and \@outputValue1 need to remain adjacent, but I'm randomising the order so that input/output pair 1 is not necessarily adjacent to input/output pair 2.

--------------------------------------------------------------

g0n, backpropagated monk

Replies are listed 'Best First'.
Re: Randomising the order of an array
by ikegami (Patriarch) on Jul 19, 2005 at 18:44 UTC

    What about

    use List::Util qw( shuffle ); my @jumpedArray = map { $dataArray[$_*2+0], $dataArray[$_*2+1] } shuffle 0 .. @dataArray/2-1;

     

    Off topic: Wouldn't it be easier if your data was organized as follows:

    my @dataArray = ( [ \@inputValue1, \@outputValue1 ], [ \@inputValue2, \@outputValue2 ], ... );

    That would eliminate any unsightly $_*2+0 and $_*2+1 you have in your code. For example, the shuffling would simplify to the following:

    use List::Util qw( shuffle ); my @jumpedArray = map { $dataArray[$_] } shuffle 0 .. $#dataArray;

     

    Update: oops, I was doing a straight copy. Fixed.

Re: Randomising the order of an array
by brian_d_foy (Abbot) on Jul 19, 2005 at 21:28 UTC
Re: Randomising the order of an array
by JediWizard (Deacon) on Jul 19, 2005 at 18:38 UTC

    Would not List::Util's shuffle method work?


    They say that time changes things, but you actually have to change them yourself.

    —Andy Warhol

Re: Randomising the order of an array
by mrborisguy (Hermit) on Jul 19, 2005 at 18:40 UTC

    I like to do this to randomize an array:

    my @shuffled = map { $_->[1] } sort { $a->[0] <=> $b->[0] } map { [rand(),$_] } @unshuffled;

    Update: This wouldn't work (see OP's update)

        -Bryan

      Your solution is not guaranteed to be well shuffled since you relying on sort to shuffle ties.

      By using sort, you've converted a O(N) problem into an O(N log N) problem. That can make a substantial difference in execution time. In other words, your solution is less scalable.

      Something which isn't scalable could still be fast for smaller input set, but it's not even the case here:

      >perl script.pl 50 Rate mrborisguy ikegami mrborisguy 2074/s -- -51% ikegami 4225/s 104% -- # Without XS. Rate mrborisguy ikegami mrborisguy 2167/s -- -91% ikegami 23688/s 993% -- # With XS.

      And for an array of 1700, as relevant here:

      >perl script.pl 1700 Rate mrborisguy ikegami mrborisguy 31.0/s -- -74% ikegami 120/s 286% -- # Without XS. Rate mrborisguy ikegami mrborisguy 31.7/s -- -96% ikegami 722/s 2179% -- # With XS.

      The benchmark script:

      Update: Added benchmarks to back my statements.

        Your solution is not guaranteed to be well shuffled since you relying on sort to shuffle ties.
        But you do know the period of repetition for rand(), right? ;-)