in reply to Re: Random Password Generator
in thread Random Password Generator

It is simpler than many, but still unecessarily complicated, because of the amount of repeated code.

Presuming you want 3 each of the lowercase, uppercase, and digits, you can simplify the code to:

my @ranges = ([a..z]) x 3, ([A..Z]) x 3, ([0..9]) x 3; my $password; while (@ranges) { $range = splice @ranges, rand @ranges, 1; # random pick $password .= $range->[rand @$range]; # random pick from the range }
The whole thing of $password = join "", $password, ... in the original post is definitely worth unlearning if nothing else. {grin}

-- Randal L. Schwartz, Perl hacker
Be sure to read my standard disclaimer if this is a reply.

Replies are listed 'Best First'.
Re^3: Random Password Generator
by cosimo (Hermit) on Jul 01, 2005 at 15:42 UTC

    Or even simpler:

    @chr = (0..9,'A'..'Z','a'..'z'); print @chr[rand @chr] for 1..9;

    but you don't get anymore the 3-3-3 pattern, which IMHO, isn't a big deal anyway.

      but you don't get anymore the 3-3-3 pattern, which IMHO, isn't a big deal anyway.

      Depending on what you're using the passwords for it could be a big deal since the 3-3-3 pattern dramatically reduces the space of possible passwords, making a brute force attack more feasible.

      Restricting the format of passwords is almost always a bad idea security wise.

Re^3: Random Password Generator
by satz (Initiate) on Jul 02, 2005 at 04:22 UTC
    This was actually a little experiment. I know the more practical way would be to use arrays, like in your post. I was just trying to find the best way to do it using only strings. :o)
Re^3: Random Password Generator
by Anonymous Monk on Jul 03, 2005 at 19:05 UTC
    I'm the original anon poster.. (-- I'll make an account one of these days) and umm.. what variable in your improvement is the password at the end? I can't seem to work it out. I know, I know. I'm dumb.