Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

Motivation

Until I saw this module, String::Random I have been using clunky ways to generate random text, always having to lookup ascii codes. This module is an excellent way to generate random strings from a template of possible characters.

Instant Random Strings

use String::Random; $foo = new String::Random; # e.g. AqF8, YcE2, BjW8 ... $string = $foo->randpattern("CcCn");

yields a random four character string: uppercase letter, lowercase, uppercase and then a number. Need a license plate? randpattern("CnCnCn") works for where I live

Smarter Random Strings

Particularly useful is the ability to define sets of characters and assign them to a pattern

# define vowels $foo->{'V'} = [ qw(a e i o u) ]; # define common consonants $foo->{'Q'} = [ qw(r s t n m) ]; # e.g. retom, satan, timis ... $string = $foo->randpattern("QVQVQ");

particularly useful for generating names of MUD characters.

Random Regex

The module also accepts a regex as input.

# e.g. 342, 289, 832 ... print $foo->randregex('\d\d\d');

My Own Usage

  • generate short random sequences of DNA using $foo->{'V'} = [ qw(a t g c) ]; to define the four base pairs
  • generate passwords for users that are relatively easy to remember, like $foo->randpattern("!QVCVQn") which gives .saZem9 +nicot8 and so on
  • random file names without all the randomness of using Digest::MD5

This is one of those small-tool modules that you may find yourself using over and over again.

Edit: chipmunk 2001-06-18


In reply to String::Random by ishmael

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    [shmem]: eclipse? Java? omg...!
    [chacham]: The darkness here is quite natural. Sunset was 1.5 hours ago ...

    How do I use this? | Other CB clients
    Other Users?
    Others making s'mores by the fire in the courtyard of the Monastery: (4)
    As of 2017-08-21 18:17 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      Who is your favorite scientist and why?



























      Results (324 votes). Check out past polls.

      Notices?