http://www.perlmonks.org?node_id=425717

EchoAngel has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks, I was looking to create this unique random string mix with numbers and letters. I was thinking of a length of 10-20. How can I do this?
  • Comment on Perl : How to Create Unique String/Number?

Replies are listed 'Best First'.
Re: Perl : How to Create Unique String/Number?
by Fletch (Bishop) on Jan 27, 2005 at 20:04 UTC

    Data::UUID

    Update: Just to clarify, this is probably what you want to use rather than just a random string if uniqueness is important; with the random solutions you'll need to keep a canonical list and make sure there's no dupes yourself. The UUID scheme shouldn't make a dupe of anyone else's value for another 1395-ish years if you set it up correctly.

Re: Perl : How to Create Unique String/Number?
by fglock (Vicar) on Jan 27, 2005 at 19:41 UTC
    use Digest::MD5; print Digest::MD5::md5_base64( rand ), "\n"; # BxaMo0Fkirbh1A+9JbbvjQ

    update: see String::Random

Re: Perl : How to Create Unique String/Number?
by amw1 (Friar) on Jan 27, 2005 at 20:12 UTC
    While a random string _may_ be unique there's nothing that really guarantees(sp?) that it will be. UUID's however are. There is a perl module Data::UUID that will generate these things for you.

    I have not used the module but I have used uuid's in the past for unique identifiers.

    man uuidgen may give you more info.

Re: Perl : How to Create Unique String/Number?
by Roy Johnson (Monsignor) on Jan 27, 2005 at 19:44 UTC
    my @alphabet = (('a'..'z'), 0..9); my %collection; for (1..30) { my $len = rand(11) + 10; my $key = join '', map {$alphabet[rand(@alphabet)]} 1..$len; $collection{$key} ? redo : $collection{$key}++; } print join "\n", keys %collection;

    Caution: Contents may have been coded under pressure.
Re: Perl : How to Create Unique String/Number?
by friedo (Prior) on Jan 27, 2005 at 20:13 UTC
    I usually use something like this:

    my @a = map { chr } (33..126); # printable ASCII chars my $string; $string .= $a[rand(@a)] for 1..20;
Re: Perl : How to Create Unique String/Number?
by sleepingsquirrel (Chaplain) on Jan 27, 2005 at 19:53 UTC
    Might not be exactly what you're looking for, but maybe a place to start.
    #!/usr/bin/perl -w $len = 11 + int(rand(10)); $r .= chr(48 + rand(74)) for (1..$len); $r =~ s/\W//g; print "$r\n";


    -- All code is 100% tested and functional unless otherwise noted.
Re: Perl : How to Create Unique String/Number?
by TedPride (Priest) on Jan 27, 2005 at 21:29 UTC
    Depending on what your needs are, a MD5 hash on time and user IP is probably best. The time-IP combination is not repeatable, and MD5 has an almost certain probability of creating a unique hash given a unique input.
      The time-IP combination is not repeatable
      Time to what granularity? If you're talking to second-level granularity, I'll have to disagree. What about requests coming from behind a proxy? All of the internet traffic from my place of work seems to come from the same IP (and for all intents and purposes it does) regardless of the computer that's actually issuing the request. What happens when a lot of traffic is generated from one such place? If you think it can't happen, think again. Data::UUID is the way to go. Smarter people than you and I have thought about this problem and come up with that solution.

      thor

      Feel the white light, the light within
      Be your own disciple, fan the sparks of will
      For all of us waiting, your kingdom will come

        mmm, what characters should I not put because I want to generate an unique string that would be used for naming files?