Random String Generator

Random String Generator

Post by DMB » Thu, 17 Mar 2005 05:53:26


I need to write a Perl function that returns a 32 character randomly
generated string of characters.

I tried the following with success, but I thought someone out there
might have a cleaner way to do this.

my @c = qw(A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 0 1 2 3
4 5 6 7 8 9);

my $ID = $c[rand @c].$c[rand @c].$c[rand @c].$c[rand @c].$c[rand
@c].$c[rand @c].$c[rand @c].$c[rand @c].$c[rand @c].$c[rand @c].$c[rand
@c].$c[rand @c].$c[rand @c].$c[rand @c].$c[rand @c].$c[rand @c].$c[rand
@c].$c[rand @c].$c[rand @c].$c[rand @c].$c[rand @c].$c[rand @c].$c[rand
@c].$c[rand @c].$c[rand @c].$c[rand @c].$c[rand @c].$c[rand @c].$c[rand
@c].$c[rand @c].$c[rand @c].$c[rand @c];
 
 
 

Random String Generator

Post by Paul Lall » Thu, 17 Mar 2005 06:05:53


@c].$c[rand
@c].$c[rand
@c].$c[rand
@c].$c[rand

#!/usr/bin/perl
use strict;
use warnings;
my @c = qw(A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 0 1 2 3 4
5 6 7 8 9);
my $ID = '';
for (1..32){
$ID .= $c[rand @c];
}

__END__

Paul Lalli

 
 
 

Random String Generator

Post by Paul Lall » Thu, 17 Mar 2005 06:10:07


4

This, of course, can be shortened to:
my @c = ('A'..'Z', '0'..'9');
 
 
 

Random String Generator

Post by Big and Bl » Thu, 17 Mar 2005 06:45:59


my @c = qw(A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
0 1 2 3 4 5 6 7 8 9);

my $ID = ' 'x32;
for (0..31){
substr($ID, $_, 1) = $c[rand @c];
}


--
Just because I've written it doesn't mean that
either you or I have to believe it.
 
 
 

Random String Generator

Post by John W. Kr » Thu, 17 Mar 2005 07:04:16


my @c = ( 'A' .. 'Z', 0 .. 9 );

my $ID = join '', map $c[ rand @c ], 1 .. 32;



John
--
use Perl;
program
fulfillment
 
 
 

Random String Generator

Post by Fabian Pil » Thu, 17 Mar 2005 07:18:04


By replacing your for loop with a map call, you could write the string
generation down in one line ;-)

my @c = ( 'A' .. 'Z', '0' .. '9' );
my $ID = join '', map $c[rand @c], 1 .. 32;

regards,
fabian
 
 
 

Random String Generator

Post by Matthew Br » Thu, 17 Mar 2005 10:23:41


If you don't mind only having hex characters (which helps* avoid
unwanted and unexpected phrases popping up in your string...):

my $id = sprintf("%08X%08X%08X%08X",
rand(4294967295),
rand(4294967295),
rand(4294967295),
rand(4294967295));

Also limits the number of calls to rand, so should be faster.

MB

* Not a complete solution to that though
 
 
 

Random String Generator

Post by Mark Cleme » Thu, 17 Mar 2005 15:30:40


Other solutions to this have already been posted, *but* bear in mind
that rand produces pseudo-random numbers that aren't suitable for use in
eg cryptography. You may want to check out Crypt::Random (I used to use
Math::TrulyRandom but it doesn't look like that is being actively
maintained).

Mark
 
 
 

Random String Generator

Post by DMB » Thu, 17 Mar 2005 23:58:30

Now that's cool! Thanks.
 
 
 

Random String Generator

Post by A. Sinan U » Fri, 18 Mar 2005 00:32:41


@o13g2000cwo.googlegroups.com:


What is cool?

Please provide context when you post.

Please do read the posting guidelines for this group.

Sinan
 
 
 

Random String Generator

Post by Richard Gr » Fri, 18 Mar 2005 01:07:19


This thread has been done to death now, but I think this solution is
rather elegant. Needless to say I did not invent it. I originally found it
in Wing, the webmail app, but I don't know if it originates with Wing's
author ...

# It's important that there are 64 chars in this array ...
my @session_chars = ('A' .. 'Z', 'a' .. 'z', 0 .. 9, '.', '-');
my $raw_rand;
open(RANDOM,"/dev/urandom") or die "Couldn't open /dev/urandom: $!";
FORKED: {
redo FORKED if (read(RANDOM,$raw_rand,$length) != $length);
$raw_rand =~ s/(.)/$session_chars[ord($1) & 63]/esg;
}
close (RANDOM);

Rich
 
 
 

Random String Generator

Post by Richard Gr » Fri, 18 Mar 2005 01:08:14


This thread has been done to death now, but I think this solution is
rather elegant. Needless to say I did not invent it. I originally found it
in Wing, the webmail app, but I don't know if it originates with Wing's
author ...

# It's important that there are 64 chars in this array ...
my @session_chars = ('A' .. 'Z', 'a' .. 'z', 0 .. 9, '.', '-');
my $raw_rand;
my $length = 32;
open(RANDOM,"/dev/urandom") or die "Couldn't open /dev/urandom: $!";
FORKED: {
redo FORKED if (read(RANDOM,$raw_rand,$length) != $length);
$raw_rand =~ s/(.)/$session_chars[ord($1) & 63]/esg;
}
close (RANDOM);

Rich
 
 
 

Random String Generator

Post by DMB » Sat, 19 Mar 2005 05:08:55

Isn't it obvious? I didn't post randomly within the thread. I
specifically posted a response to Fabian Pilkowski's post. Had I meant
that more than just his suggestion was good, I would have listed them
specifically. If you are having a conversation with 4 people and one
looks at you directly and tells you something, then you respond with
"that's cool", doesn't it seem logical that your response was to his
statement and not to something that one of the other 3 stated earlier.

Anyway, it's not a big deal. I was justing posting a quick memo to let
Fabian know that I appreciated his suggestion. My post didn't add any
value to the thread other than that, so I didn't see any reason to be
thorough.
 
 
 

Random String Generator

Post by A. Sinan U » Sat, 19 Mar 2005 05:17:06


@g14g2000cwa.googlegroups.com:


Isn't what obvious?


"It", i.e. not composing your follow-ups, is a big deal.


Precisely.

Sinan
 
 
 

Random String Generator

Post by Jeff » Sat, 19 Mar 2005 05:45:49


Whatever 'it' is, I guess not, since I have no idea what you're talking
about.


This is posted randomly in a thread, so I'll assume you have done so
before and you will do so again. Are you under the impression that when
reading newsgroups, everyone sees *all* the messages? Many (most?)
people only see new messages, and it is the responsibility of posters to
provide enough context within their message to make them coherent


Which somone would know exactly how?


Nope. Sometimes I've gotten lost in thought for a couple of minutes,
and I'm responding to what someone said a few minutes ago. And posting
on Usenet is not like speaking to people in person anyway. In the
absence of body language, *** expressions, subtlety of vocal
inflection, and time continuity, it's much more important to make
yourself clear and easily understood.


I guess if you don't want anyone to know what you're talking about, it
isn't.


If you consistently demonstrate you are not thorough, and you don't
follow established posting guidelines, people will stop reading your
messages. Whether that is a good enough reason to be thorough is your
own decision to make.

~Jeff