in reply to Randomize word
The process you describe is a shuffle. If you don't want to use List::Util which AFAIK is not core (for <5.8 ) then you could just use a Fisher Yates shuffle like this:
sub random { my @array = split //, shift; for (my $i = @array; --$i; ) { my $j = int rand ($i+1); @array[$i,$j] = @array[$j,$i]; } return join '', @array; } print random('hello');
UpdateAristotle notes that List::Util is core from 5.8.
cheers
tachyon
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Randomize word
by Roy Johnson (Monsignor) on Sep 07, 2004 at 14:34 UTC | |
|
Re^2: Randomize word
by Aristotle (Chancellor) on Sep 07, 2004 at 15:41 UTC |