Random String Generator

Discussion in 'Perl Misc' started by DMB, Mar 15, 2005.

  1. DMB

    DMB Guest

    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];
    DMB, Mar 15, 2005
    #1
    1. Advertising

  2. DMB

    Paul Lalli Guest

    "DMB" <> wrote in message
    news:...
    > 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
    Paul Lalli, Mar 15, 2005
    #2
    1. Advertising

  3. DMB

    Paul Lalli Guest

    "Paul Lalli" <> wrote in message
    news:R6IZd.5051$aS5.536@trndny05...
    > 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
    >
    Paul Lalli, Mar 15, 2005
    #3
  4. DMB

    Big and Blue Guest

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


    --
    Just because I've written it doesn't mean that
    either you or I have to believe it.
    Big and Blue, Mar 15, 2005
    #4
  5. DMB wrote:
    > 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
    --
    use Perl;
    program
    fulfillment
    John W. Krahn, Mar 15, 2005
    #5
  6. * Paul Lalli wrote:
    >
    > > 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
    Fabian Pilkowski, Mar 15, 2005
    #6
  7. DMB wrote:
    > 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
    Matthew Braid, Mar 16, 2005
    #7
  8. DMB wrote:
    > 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
    Mark Clements, Mar 16, 2005
    #8
  9. DMB

    DMB Guest

    Now that's cool! Thanks.
    DMB, Mar 16, 2005
    #9
  10. "DMB" <> wrote in news:1110985110.078483.177460
    @o13g2000cwo.googlegroups.com:

    > Now that's cool! Thanks.


    What is cool?

    Please provide context when you post.

    Please do read the posting guidelines for this group.

    Sinan
    A. Sinan Unur, Mar 16, 2005
    #10
  11. On Tue, 15 Mar 2005 12:53:26 -0800, DMB wrote:

    > 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
    Richard Gration, Mar 16, 2005
    #11
  12. On Tue, 15 Mar 2005 12:53:26 -0800, DMB wrote:

    > 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
    Richard Gration, Mar 16, 2005
    #12
  13. DMB

    DMB Guest

    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.
    DMB, Mar 17, 2005
    #13
  14. "DMB" <> wrote in news:1111090135.855962.176190
    @g14g2000cwa.googlegroups.com:

    > Isn't it obvious?


    Isn't what obvious?

    > Anyway, it's not a big deal.


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

    > My post didn't add any value to the thread


    Precisely.

    Sinan
    A. Sinan Unur, Mar 17, 2005
    #14
  15. DMB

    Jeff Guest

    DMB wrote:
    > 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
    Jeff, Mar 17, 2005
    #15
  16. DMB

    Keith Keller Guest

    On 2005-03-17, DMB <> wrote:
    > 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

    --
    -francisco.ca.us
    (try just my userid to email me)
    AOLSFAQ=http://wombat.san-francisco.ca.us/cgi-bin/fom
    see X- headers for PGP signature information
    Keith Keller, Mar 17, 2005
    #16
  17. DMB wrote:

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

    --
    Cocoa programming in Perl: http://camelbones.sourceforge.net
    Hire me! My resume: http://www.dot-app.org
    Sherm Pendley, Mar 17, 2005
    #17
  18. DMB

    Scott Bryce Guest

    DMB wrote:
    > 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.
    Scott Bryce, Apr 17, 2005
    #18
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Martin Maurer
    Replies:
    3
    Views:
    4,839
    Peter
    Apr 19, 2006
  2. Phipps Xue

    random string generator library!

    Phipps Xue, Jan 9, 2004, in forum: C++
    Replies:
    2
    Views:
    3,255
    Sasha Goldshtein
    Jan 9, 2004
  3. Jonathan Burd

    Thread-Safety in Functions: A Random String Generator

    Jonathan Burd, Jan 6, 2005, in forum: C Programming
    Replies:
    4
    Views:
    485
  4. globalrev
    Replies:
    4
    Views:
    757
    Gabriel Genellina
    Apr 20, 2008
  5. VK
    Replies:
    15
    Views:
    1,161
    Dr J R Stockton
    May 2, 2010
Loading...

Share This Page