For Programmers: Free Programming Magazines  


Home > Archive > PERL CGI Beginners > January 2006 > Random Optimized Playlist suggestion wanted.









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Random Optimized Playlist suggestion wanted.
Stass

2006-01-27, 3:55 am

Can someone suggest an optimized algorythm of the random playlist?
I can make it through array - but it is not good in my mind to rearrange
the array after element delete...

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]


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com