in reply to Re: Randomize word
in thread Randomize word

That wouldn't work if the word to shuffle contains duplicate letters.

Replies are listed 'Best First'.
Re^3: Randomize word
by ikegami (Patriarch) on Sep 07, 2004 at 15:55 UTC

    Look again. Duplicate letters work.

    However, ActivePerl v5.6.1 build 633 actually returns unshuffled results for "rotohotel", "r1t2h3tel" and "r1t2h3tels"!! Nothing guarantees that order is lost in a hash.

    May I recommend a variation:

    $_='rotohotel'; my $len; my %f=map {rand() . '|' . $len++ => $_} split //; print join('', values %f), "\n";

    It seems values() is implemented as map { $hash{$_} } sort keys %hash) on this build!!

    Update: ok, so it's not always sorted:

    %hash = map { $_ => $_ } qw(m i f e z l x v b r d o h y j q s t k n g +c a p w u); print(join('', values(%hash)), "\n"); # prints: abcdefghijklmnopqrstuvwxyz SORTED %hash = map { $_ => $_ } qw(m i f e z l x 2 v b r d o h y j q s t k n +g c a p w u); print(join('', values(%hash)), "\n"); # prints: abcdefghijklmnop2qrstuvwxyz NOT SORTED %hash = map { $_ => $_ } qw(6 3 5 0 1 2 9 8 7 4); print(join('', values(%hash)), "\n"); # prints: 0123456789 SORTED %hash = map { $_ => $_ } qw(6 3 5 0 34 1 2 33 9 8 7 4); print(join(':', values(%hash)), "\n"); # prints: 0:1:2:3:4:5:6:7:8:9:33:34 SORTED %hash = map { $_ => $_ } qw(a aaaaaaa aaaa aaa aa aaaaa aaaaaaaaaa); print(join(':', values(%hash)), "\n"); # prints: aa:aaaaaaa:a:aaaaaaaaaa:aaaaa:aaaa:aaa NOT SORTED