in reply to help in understanding the standard array shuffle
I dont know if it's efficient, but it works =)
Hope this helps
Update
In fact, it's really a bad idea to use this for randomize an array...
See the result of this script :
# indexed array @array = sort { (-1,1)[int rand 2] } @array; # or trinary alternative @array = sort { int rand 2 ? 1 : -1 } @array;
Hope this helps
Update
In fact, it's really a bad idea to use this for randomize an array...
See the result of this script :
Return :#!/usr/bin/perl -w use strict; my @array = qw( peach banana mango orange apple cherry ); my %result; $, = "\t\t"; $\ = "\n"; for(1..10000) { @_ = sort { (-1,1)[int rand 2] } @array; for(0..$#array) { $result{ $_[$_] , $_ }++ } } print '' , (0..$#array); for my $fruit ( @array ) { print $fruit , map { $result{ $fruit , $_ } } (0..$#array); }
Not really randomized huh ? ...0 1 2 3 4 5 peach 2982 3026 2010 1095 573 314 banana 2961 2973 1960 1172 602 332 mango 1996 2004 2333 1866 1100 701 orange 1156 1116 1914 2558 1999 1257 apple 602 561 1162 2030 3171 2474 cherry 303 320 621 1279 2555 4922
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Re: help in understanding the standard array shuffle
by merlyn (Sage) on Feb 14, 2001 at 19:47 UTC | |
Re: Re: help in understanding the standard array shuffle
by kschwab (Vicar) on Feb 14, 2001 at 18:59 UTC | |
by extremely (Priest) on Feb 14, 2001 at 19:26 UTC | |
by kschwab (Vicar) on Feb 14, 2001 at 19:59 UTC | |
by extremely (Priest) on Feb 14, 2001 at 20:15 UTC | |
by arhuman (Vicar) on Feb 14, 2001 at 19:21 UTC |
In Section
Seekers of Perl Wisdom