in reply to Re^4: Generate a unique ID
in thread Generate a unique ID
I do not argue that a PRNG has a non-zero chance of repetition. But I have found, if only through empirical observation, that a reasonable implementation of this strategy is a good pragmatic solution to the collision problem.
In actual implementation, I would write a loop that attempts, say, ten times, to produce a random directory-name and to create a new directory having that name. (If the loop failed to do so, each time, it would generate a warning message to STDERR, because even one actual collision would be, in my book, quite unexpected.) The loop would not permit the directory to be used if it already existed (thus pushing the “atomicity problem” off to the file system).
The odds of even one name-collision are extremely small; the odds of ten collisions in a row are almost-infinitely smaller.
And once the program has acquired a temporary directory that is all its own, it can build whatever files it wants within that directory, and can do with them as it pleases.
Upon termination, it destroys the directory and its content.
I would probably add a short prefix to the random string, both to make it easier to recognize why a given directory-name is present in /tmp, and to simplify the process of removing them en masse.
| Replies are listed 'Best First'. | |
|---|---|
|
Re^6: Generate a unique ID
by BrowserUk (Patriarch) on Nov 17, 2010 at 15:02 UTC | |
by locked_user sundialsvc4 (Abbot) on Nov 17, 2010 at 17:22 UTC | |
by BrowserUk (Patriarch) on Nov 17, 2010 at 17:58 UTC | |
by Anonymous Monk on Nov 17, 2010 at 15:38 UTC | |
by BrowserUk (Patriarch) on Nov 17, 2010 at 15:42 UTC |