[Omaha.pm] Randomize an array

Jay Hannah jhannah at omnihotels.com
Thu Sep 30 10:34:42 CDT 2004


Neat. I can't remember every wanting to randomize an array. Now I need to to see if my message pair groking routing works under load.

60 seconds on Google, and I hit this array randomizer.

Yay!

(
It also reminded me of this class I was looking at a couple weekends ago:
http://search.cpan.org/~simon/Games-Poker-TexasHold-em-1.4/em.pm
http://use.perl.org/~petdance/journal/20904
)


j


###############################################################################
#
#   Function:   shuffle
#   Purpose:    Randomize an array
#
#   Comments:   This routine was ripped from 'Perl Cookbook' pg 121-122
#               
###############################################################################

sub shuffle {
    my $array = shift;
    my $i = scalar(@$array);
    my $j;
    foreach $item (@$array )
    {
        --$i;
        $j = int rand ($i+1);
        next if $i == $j;
        @$array [$i,$j] = @$array[$j,$i];
    }
    return @$array;
}




More information about the Omaha-pm mailing list