Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Randomise a string of letters

by Anonymous Monk
on Jan 15, 2003 at 14:16 UTC ( [id://227145]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Do any monks know of a method by which a string of letters can be reordered in a truly random order?

Replies are listed 'Best First'.
(ar0n) Re: Randomise a string of letters
by ar0n (Priest) on Jan 15, 2003 at 14:35 UTC
    use Algorithm::Numerical::Shuffle qw/shuffle/; my $random = join '', shuffle split //, "The quick brown fox";
    Though "truly random" is often a bit of a misnomer in computer land.
      or use List::Util qw/shuffle/; (rest stays the same) which has the added bonus of coming standard with perl 5.8.0.

      -- Hofmator

Re: Randomise a string of letters
by Rhose (Priest) on Jan 15, 2003 at 14:35 UTC
    Using the "Randomizing an Array" example from the Perl Cookbook, I threw together the following code. Does this help?
    #!/usr/bin/perl -w use strict; my $gStr='This is my string'; my @gStrArr=split('',$gStr); 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(\@gStrArr); $gStr=join('',@gStrArr); print $gStr,"\n";
Re: Randomise a string of letters
by bronto (Priest) on Jan 15, 2003 at 17:49 UTC

    Really verbose and a bit machiavellic, but works:

    use strict ; use warnings ; my $string = "This is a not so random string" ; my $randomized ; { my @chars = $string =~ /./g ; my %randindex ; for (my $i = 0 ; $i <= $#chars ; $i++) { $randindex{rand()} = $i ; } $randomized = join "",map $chars[$randindex{$_}], sort keys %randind +ex ; } print "$randomized\n" ;

    It seems to do what you asked for:

    $ for I in 1 2 3 4 5 ; do perl randomize_string.pl ; done dsg Tosnirtan sis r maton oih nomira sr s dh tiansiotoTgn s most g air dsi Tnrs anhnitoos tinradoo issaosh siTnt rmgn t n ssa hsgni tdrmosna Tiroio

    Ciao!
    --bronto

    # Another Perl edition of a song:
    # The End, by The Beatles
    END {
      $you->take($love) eq $you->make($love) ;
    }

Re: Randomise a string of letters
by BrowserUk (Patriarch) on Jan 15, 2003 at 21:38 UTC

    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.

      Does anyone know how all these approaches compare with the EMBOSS program shuffleseq?

        I don't know for sure, but I assume (on the basis of what you have said in other threads) that EMBOSS is a gene sequence manipulation program/suite.

        Given that genes sequences tend to be represented by pairs of chars, it quite probable that the routine you mention shuffles pairs of bytes around.

        It's also quite likely, though again I don't really know, that this would be done in C, which is probably much faster that any implementation in pure perl. Though it's possible that one of the modules above implements the shuffle using XS or C in which case they could be comaparable for performance.

        In terms of "true randomness", provided the shuffle is reasonable well written, the randomness will come down to the randomness of the RNGenerator, and unless your involved in crytoanalysis or heavy statistical analyisis, the RNG on most systems is good enough for most purposes.


        Examine what is said, not who speaks.

        The 7th Rule of perl club is -- pearl clubs are easily damaged. Use a diamond club instead.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://227145]
Approved by jmcnamara
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (7)
As of 2024-03-29 13:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found