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

Re: Re: String randomness

by Arien (Pilgrim)
on Aug 16, 2002 at 09:02 UTC ( [id://190616]=note: print w/replies, xml ) Need Help??


in reply to Re: String randomness
in thread String randomness

Why would you select random characters with values ranging from 0 to 121 and later delete the ones that you didn't want to use in the first place, when you already know with which characters (the ones in the string $OK_CHARS) you want to build your random word?

You could just stick the characters you want to use in an array and pick the required number of random elements from the array, like so:

sub randomPassword { my $length = shift || 12; my @OK_CHARS = ('a'..'z', 'A'..'Z', '0'..'9', qw[_ - . @]); my $password; for (1 .. $length) { $password .= $OK_CHARS[ int rand scalar @OK_CHARS ]; } return $password; }

— Arien

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (2)
As of 2025-02-09 02:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Which URL do you most often use to access this site?












    Results (95 votes). Check out past polls.