Random string

| Perl | 6 Comments

Speaking of elegant ways to get something complicated done with a minimum amount of good-old perling, here's an interesting algorithm:

sub random_string
{
  my $max = shift;
  return join '', map { ('a'...'z', '0'...'9', 'A'..'Z', '0'...'9')[rand 72] } (1..$max);
}

In case you couldn't figure it out yourself, it takes a single integer as input and generates a string of that many characters made up of random upper- and lowercase letters and digits.

6 Comments

Why do you use the ... operator instead of ..? And why have 2 sets of digits?

Strange, I posted a comment last week. Hasn't it arrived?

Sorry, but for some reason my junk filter went haywire and was banning all comments for the last couple of weeks.

I put in the range of digits twice because I figured that the random choice would otherwise be too skewed towards preferring characters over digits.

Yes, I should have used two dots instead of three. Sorry. Funny thing is: it still works!

The algorithm still slightly favours letters over digits (26 letters to 20 digits). The following routine may not be as short (although it contains enough Perlisms :-), but is not biased:

sub generate_password {
    my $length = shift || 8;
    my $password = "";
    my @valid_chars = ( ['a' .. 'z', 'A' .. 'Z'], ['0' .. '9'] );
    my @lengths = map { scalar @{$valid_chars[$_]}-1 } 0 .. $#valid_chars;

    for (1 .. $length) {
        my $type = rand 2;
        $password .= $valid_chars[ $type ]->[ rand $lengths[$type] ];
    }
    return $password;
}

The three dots still work because in list context, they behave exactly as the double dot operator. Only in boolean context they behave differrently. It's documented in the "Range Operators" section of the perlop documentation.

By the way, posting code should be easier :-)

I guess I am always a stickler for terse code over code that is complete. The more concise the more elegant, that is as long as it is still readable. However, I'd choose your algorithm above mine if judging completeness. Nice job.

I'd be more concerned about the correctness than about the completeness. If a function is called "random_string" and the strings generated aren't random, then what is the use of completeness?

Of course it also depends on the actual use of the routine, but the original algorithm, however elegant and clever, would not be suitable for "production use" IMHO.

Random entries

Here are some random entries that you might be interested in:

Recent Assets

  • tegen-par-2024-2nd-place.jpeg
  • stanford-reunion.png
  • kiff.png
  • hoid.png
  • Dad-in-front-of-log-cabin.png
  • mistborn-trilogy.png
  • 2024-03-Heren1-27h.png
  • three-body-problem.png
  • 10CC.png
  • minds-and-machines.jpeg
  • puglia.png
  • 2023-09-24-jong-tegen-oud-1.jpg

Recent Comments

  • Random string: I'd be more concerned about the correctness than a ...
    - Arjen Laarhoven
  • Random string: I guess I am always a stickler for terse code over ...
    - Kiffin
  • Random string: The algorithm still slightly favours letters over ...
    - Arjen Laarhoven
  • Random string: Sorry, but for some reason my junk filter went hay ...
    - Kiffin
  • Random string: Strange, I posted a comment last week. Hasn't it ...
    - Arjen Laarhoven

Golf Handicap

Information

This personal weblog was started way back on July 21, 2001 which means that it is 7-21-2001 old.

So far this blog contains no less than 2518 entries and as many as 1877 comments.

Important events

Graduated from Stanford 6-5-1979 ago.

Kiffin Rockwell was shot down and killed 9-23-1916 ago.

Believe it or not but I am 10-11-1957 young.

First met Thea in Balestrand, Norway 6-14-1980 ago.

Began well-balanced and healthy life style 1-8-2013 ago.

My father passed away 10-20-2000 ago.

My mother passed away 3-27-2018 ago.

Started Gishtech 04-25-2016 ago.