Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question: (data formatting)

How to generate a unique word or number?

Originally posted as a Categorized Question.

  • Comment on How to generate a unique word or number?

Replies are listed 'Best First'.
Re: How to generate a unique word or number?
by japhy (Canon) on Sep 29, 2000 at 16:00 UTC

    Here's one way. The sequence of strings it produces is non-repeating, and, though very long, not infinite.

    { my $UNIQ_ID = 'a'; sub next_id { return $UNIQ_ID++ } }
Re: How to generate a unique word or number?
by Adam (Vicar) on Sep 29, 2000 at 20:18 UTC

    $str = time();
    Values will be unique, as long as you only need a new number every couple of seconds.

    But seriously, if need this for any kind of security purpose, then you want something that isn't predictable either. There was a good discussion of this at Randomizing Unique ID?. I'll boil it down for you:

    Don't re-seed (as discussed here). The default seed is very good, in versions of Perl since 5.004.

    If you do choose to re-seed, incorporate the previous seed:

    srand( rand(~0) ^ $mySeed );

    That said, use some combination of time, PID (Process ID, or $$), and rand.

    Randomizing Unique ID? also contemplates changing the base of the number (making it a string); this resulted in the Base Conversion Utility.

    Unfortunately even this scheme is not perfect. You have a couple issues:

    • How many unique strings are you trying to make?
    • Are they from the same run of the script, or different runs?
      $$ won't change in one run of a script.
    • How often is it run?
      time only provides resolution down to the second.
      It also can go backwards.*
      On Win32 you have Win32::GetTickCount(), but that repeats every month or so.
    • There's a chance, ever so slight, that rand will repeat itself when time and pid have not changed.
    If this all occurs within one process, then simply tacking on a counter will guarantee uniqueness.

    So the bottom line is, use as much of the output from rand as you can, to reduce the probability of a repeated string.


    * Yes, time can go backwards.
    "How?"
    Well, time uses the system clock, which has a tendency to drift. There are several mechanisms in use to keep the clock right. Some of these methods attempt to return the clock to some standard (UTC) by manually manipulating the drift (changing the number of ticks/sec to get the clock to speed up or slow down). But some systems actually re-set the clock to UTC. If the clock was running fast this re-set will move time backwards.

Re: How to generate a unique word or number?
by clemburg (Curate) on Sep 30, 2000 at 21:06 UTC
Re: How to generate a unique word or number?
by spx2 (Deacon) on Mar 15, 2008 at 00:01 UTC
    I would approach the problem in a simple manner

    sub generate_random_string { my $res = ''; $res.= chr int rand(26)+96 for 1..8; return $res; }
    Use this to generate a random string(with 8 letters).
    After this you can just use a hash/hashref to assure no new string generated
    is a duplicate with a past one

    You can do this like this
    my $h={}; sub generate_new_unique_random_str{ my $result; while(exists $h->{$result=generate_random_string()}) {}; return $result; }

    The code above will store in $result each time the value returned by our
    random string generator,and if it is not beeing found in the hash then
    the loop terminates and the new generated value is returned

      The operative word is "unique" though not "random." So I think one of the UID generating modules, like Data::UUID (and there are two or three others IIRC), would be the right answer.

Re: How do I generate a unique word or number?
by AgentM (Curate) on Sep 29, 2000 at 19:15 UTC
    Dumb solution: hash all of the previous results. Test the hash every time if the value has been used before and generate another num if it has. In any case, this solution will provide better results than storing the vals in an array . (obviously)

    Originally posted as a Categorized Answer.

Re: How do I generate a unique word or number?
by merlyn (Sage) on Sep 29, 2000 at 10:00 UTC
    How unique does it need to be? What's the universe of possible strings, and the set of already taken strings? Just pick something from the difference set, at random. {grin}

    Originally posted as a Categorized Answer.

Re: How to generate a unique word or number?
by spx2 (Deacon) on Mar 15, 2008 at 00:24 UTC
    I would approach the problem in a simple manner

    sub generate_random_string { my $res = ''; $res.= chr int rand(26)+96 for 1..8; return $res; }
    Use this to generate a random string(with 8 letters).
    After this you can just use a hash/hashref to assure no new string generated
    is a duplicate with a past one

    You can do this like this
    my $h={}; sub generate_new_unique_random_str{ my $result; while(exists $h->{$result=generate_random_string()}) {}; return $result; }

    The code above will store in $result each time the value returned by our
    random string generator,and if it is not beeing found in the hash then
    the loop terminates and the new generated value is returned

    Originally posted as a Categorized Answer.

Re: How to generate a unique word or number?
by spx2 (Deacon) on Mar 15, 2008 at 00:26 UTC
    I would approach the problem in a simple manner

    sub generate_random_string { my $res = ''; $res.= chr int rand(26)+96 for 1..8; return $res; }
    Use this to generate a random string(with 8 letters).
    After this you can just use a hash/hashref to assure no new string generated
    is a duplicate with a past one

    You can do this like this
    my $h={}; sub generate_new_unique_random_str{ my $result; while(exists $h->{$result=generate_random_string()}) {}; return $result; }

    The code above will store in $result each time the value returned by our
    random string generator,and if it is not beeing found in the hash then
    the loop terminates and the new generated value is returned

    Originally posted as a Categorized Answer.

Re: How to generate a unique word or number?
by hossman (Prior) on Jul 01, 2007 at 19:11 UTC