Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Randomise a string of letters

by bronto (Priest)
on Jan 15, 2003 at 17:49 UTC ( [id://227169]=note: print w/replies, xml ) Need Help??


in reply to Randomise a string of letters

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) ;
}

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (5)
As of 2024-04-20 01:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found