| Ing. Branislav Gerzo 2006-01-27, 3:55 am |
| Stass [S], on Friday, January 27, 2006 at 12:21 (+0600) wrote the
following:
S> Can someone suggest an optimized algorythm of the random playlist?
S> I can make it through array - but it is not good in my mind to rearrange
S> the array after element delete...
if you know shuffle good algorythm such as:
# fisher_yates_shuffle( \@array ) : generate a random permutation
# of @array in place
sub fisher_yates_shuffle {
my $array = shift;
my $i;
for ($i = @$array; --$i; ) {
my $j = int rand ($i+1);
next if $i == $j;
@$array[$i,$j] = @$array[$j,$i];
}
}
fisher_yates_shuffle( \@array ); # permutes @array in place
(^^from Oreilly Perl Bookshelf)
you can always remove undef elements from array. Also I suggest you
read:
perldoc -f splice
--
How do you protect mail on web? I use http://www.2pu.net
["Freedom cannot be legislated" - M. Muir, Suicidal Tendencies]
|