Python Forum

Full Version: generating random string unique forever
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
i want to run many processes in parallel that can generate a random-like string that is absolutely unique forever. well, forever is a long time. the string needs to be unique over the course of a long time, across as many processes a host can run in that time. maybe this time should be a year. the processes, no matter how long they are delayed, must have an absolutely unique string suitable for naming a resource in a common name space such as a file system. there must never be a collision. what callable function or method could do this? this must work without creating an additional process in any way (no invoking a command). it must be portable across major platforms.
https://en.wikipedia.org/wiki/Universall...identifier

>>> import uuid
>>> uuid.uuid1()
UUID('7bd82050-0084-11ec-9454-acde48001122')
If you might run two at once on the same machine, then just go random.

>>> uuid.uuid4()
UUID('b49c5e72-205a-4495-811f-d33b134db2a2')
How about using the current time?

import time

random_sting = str (time.time ()).replace ('.', '')
print (random_sting)
is time.time() unique across multiple processes calling it at the same instant? i suppose i could tuple it with the process ID.
(Aug-19-2021, 01:33 AM)Skaperen Wrote: [ -> ]is time.time() unique across multiple processes calling it at the same instant? i suppose i could tuple it with the process ID.

My experience is no. If you might be running multiple on one host, just use uuid4().
You already posted a similar thread about one year ago! The answer is still the same.