in reply to Randomise a string of letters
This F-Y shuffles the string in-place avoiding the split and join. It might be a bit more efficient, but I haven't benchmarked it.
#! perl -slw use strict; sub shuffleStr { my $len = length $_[0]; my ($tmp, $n); $n = $_+rand($len-$_) , $tmp = substr( $_[0], $_, 1) , substr( $_[0], $_, 1) = substr( $_[0], $n , 1) , substr( $_[0], $n , 1) = $tmp for 0 .. $len; $_[0]; } my $string = 'the quick brown fox'; print shuffleStr($string) for 1..10; __END__ C:\test>227145 wconbex ftkuoiqhr xhe kbquio nfwo tcr xen iu kwoq rtobhcf h ftcx wikboqnreou eoqkxhitfwun ocrb tbikr nuoqofwxh c e rhtwi efuc kxobnq o noit oquxhke bwcfr qruwoox eftnih kcb oo qwnfet bcxurih k C:\test>
Examine what is said, not who speaks.
The 7th Rule of perl club is -- pearl clubs are easily damaged. Use a diamond club instead.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Randomise a string of letters
by matth (Monk) on Jan 16, 2003 at 11:43 UTC | |
by BrowserUk (Patriarch) on Jan 16, 2003 at 12:32 UTC |