⭐ in reply to How to generate a unique word or number?
Values will be unique, as long as you only need a new number every couple of seconds.$str = time();
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:
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.
|
|---|