Random String Generator

D

DMB

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];
 
P

Paul Lalli

DMB said:
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];

#!/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
 
P

Paul Lalli

Paul Lalli said:
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);

This, of course, can be shortened to:
my @c = ('A'..'Z', '0'..'9');
my $ID = '';
for (1..32){
$ID .= $c[rand @c];
}

__END__

Paul Lalli
 
B

Big and Blue

DMB said:
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];

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];
}
 
J

John W. Krahn

DMB said:
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];


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

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



John
 
F

Fabian Pilkowski

* Paul Lalli said:
I need to write a Perl function that returns a 32 character randomly
generated string of characters.

#!/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__

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
 
M

Matthew Braid

DMB said:
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];

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
 
M

Mark Clements

DMB said:
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];
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
 
R

Richard Gration

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

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
 
R

Richard Gration

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

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
 
D

DMB

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.
 
J

Jeff

DMB said:
Isn't it obvious?

Whatever 'it' is, I guess not, since I have no idea what you're talking
about.
I didn't post randomly within the thread.

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
I specifically posted a response to Fabian Pilkowski's post.

Which somone would know exactly how?
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.

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, facial expressions, subtlety of vocal
inflection, and time continuity, it's much more important to make
yourself clear and easily understood.
Anyway, it's not a big deal.

I guess if you don't want anyone to know what you're talking about, it
isn't.
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.

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
 
K

Keith Keller

Isn't it obvious?

No, it's not.
I didn't post randomly within the thread. I
specifically posted a response to Fabian Pilkowski's post.

That's not immediately obvious from your post. One has to
retrieve the parent post in order to figure out to whom you
were responding.
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.

It's perfectly logical. But you're not having a conversation, you're
posting to usenet.

Consider: your "thank you" could arrive at my news server *after* some
or all of the posts which chronologically came before yours. Or, my
news server could receive three more posts before it gets yours.
Now how do I know to whom you're responding? (I need to retrieve
the parent post, and then have to hope that you picked that particular
post to followup for a reason.)
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.

So, now you know that you need to be thorough, because the way you
currently followup leaves too much ambiguity. Please quote at least
*some* relevant context next time.

--keith
 
S

Sherm Pendley

DMB said:
Isn't it obvious?

Isn't *what* obvious?

Some context in your replies, please. Not everyone has the patience to sort
through a pile of old messages just to figure out which one you're replying
to.

Have you read the posting guidelines for this group?

sherm--
 
S

Scott Bryce

DMB said:
Isn't it obvious? I didn't post randomly within the thread. I
specifically posted a response to Fabian Pilkowski's post.

But since I have my newsreader set to display messages in order by date,
and not threaded, I have no way of knowing that.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top