Beefy Boxes and Bandwidth Generously Provided by pair Networks Joe
Perl Monk, Perl Meditation.
 
PerlMonks

Re: Randomise a string of letters

by BrowserUk (Saint)
 | Log in | Create a new user | The Monastery Gates | Super Search | 
 | Seekers of Perl Wisdom | Meditations | PerlMonks Discussion | 
 | Obfuscation | Reviews | Cool Uses For Perl | Perl News | Q&A | Tutorials | 
 | Poetry | Recent Threads | Newest Nodes | Donate | What's New | 

on Jan 15, 2003 at 21:38 UTC ( #227240=note: print w/ replies, xml ) Need Help??

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.


Comment on Re: Randomise a string of letters
Download Code
Re: Re: Randomise a string of letters
by matth (Monk) on Jan 16, 2003 at 11:43 UTC
    Does anyone know how all these approaches compare with the EMBOSS program shuffleseq?
[reply]

      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.

[reply]
Login:
Password
remember me
What's my password?
Create A New User

Node Status
node history
Node Type: note [id://227240]
help
Community Ads
Chatterbox
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users
Others scrutinizing the Monastery: (8)
GrandFather
wfsp
jmcnamara
atcroft
herveus
Eyck
gnosti
im2
As of 2009-11-21 09:35 GMT
Sections
The Monastery Gates
Seekers of Perl Wisdom
Meditations
PerlMonks Discussion
Categorized Q&A
Tutorials
Obfuscated Code
Perl Poetry
Cool Uses for Perl
Perl News
Information
PerlMonks FAQ
Guide to the Monastery
What's New at PerlMonks
Voting/Experience System
Tutorials
Reviews
Library
Perl FAQs
Other Info Sources
Find Nodes
Nodes You Wrote
Super Search
List Nodes By Users
Newest Nodes
Recently Active Threads
Selected Best Nodes
Best Nodes
Worst Nodes
Saints in our Book
Leftovers
The St. Larry Wall Shrine
Offering Plate
Awards
Craft
Snippets Section
Code Catacombs
Quests
Editor Requests
Buy PerlMonks Gear
PerlMonks Merchandise
Planet Perl
Perlsphere
Use Perl
Perl.com
Perl 5 Wiki
Perl Jobs
Perl Mongers
Perl Directory
Perl documentation
CPAN
Random Node
Voting Booth

Future historians will find that the material characteristic of the current era is...

Aluminium
Plastic
Oil
Water
Carbon dioxide
Copper
Iron
Silicon
Salt
Uranium
Hydrogen
Other

Results (729 votes), past polls