Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Randomizing Unique ID?

by BlaisePascal (Monk)
on Aug 09, 2000 at 15:42 UTC ( [id://27009]=note: print w/replies, xml ) Need Help??


in reply to Randomizing Unique ID?

I dunno, would something like:
my $id = ""; for (1..18) { $id .= ('A'..'Z','a'..'z','0'..'9')[int(rand(62))];}
work for you?

Replies are listed 'Best First'.
RE: Re: Randomizing Unique ID?
by merlyn (Sage) on Aug 10, 2000 at 18:31 UTC
    Nice solution. Downsides:
    • This recreates the 62 element list repeatedly in memory, since there's no place to store it.
    • The number "62" is hardcoded, and must match the list exactly. That's a maintenance headache: suppose a requirement came along to ensure that underscore were included, or that digits were not.
    • The int is redundant.
    Implementing these three changes, I'd go with something like:
    BEGIN { my @source = ('A'..'Z', 'a'..'z', '0'..'9'); sub generate_random_password { my $id = ""; for (1..18) { $id .= $source[rand @source]; } $id; } }
    For so-called "pronounceable" passwords, try Crypt::RandPasswd. Looks nice, and based on a government standard.

    -- Randal L. Schwartz, Perl hacker

      About your downsides...

      1) Yup, I can see capturing the 62-element in a closure instead of re-creating it. I didn't think of that when I was coding off the top of my head.

      2) Nice catch

      3) Oops... I missed that. I looked in the library to verify the result of rand() and it said it returns a float, not an int. But I guess the [ takes care of that...

      Since this is supposed to be a "UniqueID" and not a password, using "pronounceable" passwords are probably not necessary.

      Why put it in a BEGIN block? Doesn't seem necessary to me.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (6)
As of 2024-03-28 08:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found