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

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.


In reply to Re: How to generate a unique word or number? by Adam
in thread How to generate a unique word or number? by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.