in reply to Random but non-repeating array loop
As japhy suggests, the Fisher-Yates shuffle is a good solution. If you want to save yourself the trouble of coding it, I've got it implemented in Math::NumberCruncher. Math::NumberCruncher also has numerous other similar functions that may be useful. Using Math::NumberCruncher, you could do the above like so:
UPDATE: Pursuant to Juerd's post, in the interest of TMTOWTDI, here's the code using Math::NumberCruncher through its original functional interface:
___________________
Kurt
use strict; use Math::NumberCruncher; my @chars = ( 'A' .. 'Z' ); my $ref = Math::NumberCruncher->new(); $ref->ShuffleArray( \@chars ); foreach my $char ( @chars ) { print $char, "\n"; }
UPDATE: Pursuant to Juerd's post, in the interest of TMTOWTDI, here's the code using Math::NumberCruncher through its original functional interface:
use strict; use Math::NumberCruncher; my @chars = ( 'A' .. 'Z' ); Math::NumberCruncher::ShuffleArray( \@chars ); foreach my $char ( @chars ) { print $char, "\n"; }
___________________
Kurt
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Re: Random but non-repeating array loop
by Juerd (Abbot) on Jan 18, 2002 at 23:48 UTC | |
by sifukurt (Hermit) on Jan 20, 2002 at 02:02 UTC |
In Section
Seekers of Perl Wisdom