Tags: generator, implement, library, machines, number, processes, programming, python, unique
unique number generator
On Programmer » Python
9,641 words with 8 Comments; publish: Wed, 26 Dec 2007 23:28:00 GMT; (20058.59, « »)
Hi,
I need to implement a unique number generator that 1 or more processes on same or different machines will make use of it. Is there any library / project available already for this?
Thanks in advance,
-- Wong
http://python.itags.org/q_python_93174.html
All Comments
Leave a comment...
- 8 Comments

- On Wed, 19 May 2004 18:36:40 +0800, "Joe Wong" <joewong.python.itags.org.mango.cc>
wrote:
> I need to implement a unique number generator that 1 or more processes on same or different machines will make use of it. Is there any library / project available already for this?
Yes. Google for GUID (globally unique identifier) algorithm or UUID
(universally unique algorithm):
"http://www.google.com/search?q=GUID+algorithm",
"http://www.google.com/search?q=UUID+algorithm".
If you need to use one, instead of implement one, and
you're running on Windows machines with Mark Hammond's
win32 extensions, you can do it easily:
import pywintypes
guid = pywintypes.CreateGuid()
have_a_nice_day(guid)
--dang
#1; Wed, 26 Dec 2007 23:30:00 GMT

- "Joe Wong" <joewong.python.itags.org.mango.cc> wrote in message news:<mailman.67.1084963007.6949.python-list.python.itags.org.python.org>...
> Hi,
> I need to implement a unique number generator that 1 or more processes
> on same or different machines will make use of it. Is there any library
> / project available already for this?
Try:
http://egenix.com/files/python/mxUID.html
or
http://www.alcyone.com/software/uid/
#2; Wed, 26 Dec 2007 23:31:00 GMT

- Hi,
There is a constraint that the number can be at most 8 digits, ie:
00000000 ~ 99999999
No hex decimal is allowed...
And I am on Linux platform..
Any other suggestion? :-)
-- Original Message --
From: "Daniel 'Dang' Griffith" <noemail.python.itags.org.noemail4u.com>
Newsgroups: comp.lang.python
To: <python-list.python.itags.org.python.org>
Sent: Wednesday, May 19, 2004 9:06 PM
Subject: Re: unique number generator
> On Wed, 19 May 2004 18:36:40 +0800, "Joe Wong" <joewong.python.itags.org.mango.cc>
> wrote:
> > I need to implement a unique number generator that 1 or more processes
on same or different machines will make use of it. Is there any library /
project available already for this?
> Yes. Google for GUID (globally unique identifier) algorithm or UUID
> (universally unique algorithm):
> "http://www.google.com/search?q=GUID+algorithm",
> "http://www.google.com/search?q=UUID+algorithm".
> If you need to use one, instead of implement one, and
> you're running on Windows machines with Mark Hammond's
> win32 extensions, you can do it easily:
> import pywintypes
> guid = pywintypes.CreateGuid()
> have_a_nice_day(guid)
> --dang
> --
> http://mail.python.org/mailman/listinfo/python-list
#3; Wed, 26 Dec 2007 23:32:00 GMT

- Daniel 'Dang' Griffith <noemail.python.itags.org.noemail4u.com> writes:
> On Wed, 19 May 2004 18:36:40 +0800, "Joe Wong" <joewong.python.itags.org.mango.cc>
> wrote:
> > I need to implement a unique number generator that 1 or more processes on same or different machines will make use of it. Is there any library / project available already for this?
> Yes. Google for GUID (globally unique identifier) algorithm or UUID
> (universally unique algorithm):
> "http://www.google.com/search?q=GUID+algorithm",
> "http://www.google.com/search?q=UUID+algorithm".
> If you need to use one, instead of implement one, and
> you're running on Windows machines with Mark Hammond's
> win32 extensions, you can do it easily:
> import pywintypes
> guid = pywintypes.CreateGuid()
> have_a_nice_day(guid)
> --dang
Pyro.util.getGUID() is a cross-platform solution. Or, you can write a
wrapper which uses MS's guid on Win** platforms, and Pyro's on *NIX
platforms.
--
harry.g.george.python.itags.org.boeing.com
6-6M21 BCA CompArch Design Engineering
Phone: (425) 342-0007
#4; Wed, 26 Dec 2007 23:33:00 GMT

- "Joe Wong" <joewong.python.itags.org.mango.cc> writes:
> Hi,
> There is a constraint that the number can be at most 8 digits, ie:
> 00000000 ~ 99999999
> No hex decimal is allowed...
> And I am on Linux platform..
> Any other suggestion? :-)
I hope you get to control all the generators for this number.
Otherwise different algorithms may accidentally overlap even if they
are properly unique in their own streams.
Given that, you need a unique number up to 1e9.
1. If you can get the traditional GUID based on MAC+timestamp+random,
you might hash that down to a digital signature in your number
range. This increases the possibility of overlaps, but it might be
acceptable in your context. E.g., do an md5 on your guid and take
the lower 7 hex digits.
2. If there are characteristics of the problem space which are
themselves unique, then you might do a canonical mapping from that
to integers. If you are lucky, this could be a dense mapping (no
skipped integers), and thus able to support a billion unique items.
--
harry.g.george.python.itags.org.boeing.com
6-6M21 BCA CompArch Design Engineering
Phone: (425) 342-0007
#5; Wed, 26 Dec 2007 23:34:00 GMT

- "Joe Wong" <joewong.python.itags.org.mango.cc> writes:
> There is a constraint that the number can be at most 8 digits, ie:
> 00000000 ~ 99999999
What an odd constraint. Is this your homework?
Your best bet is to issue increasing serial numbers from a process
that's properly mulithreaded and persistent. If you're really lazy,
install MySQL and have it generate unique IDs for you.
#6; Wed, 26 Dec 2007 23:35:00 GMT

- This is probably not easy to do, without more requirements.
UUIDs can be generated randomly, in which case about 120 of the 128 bits
may vary. This means that you wouldn't expect to generate two that are
identical before about 2^60 are generated, which is enough to make most
people comfortable.
8-digit numbers, well, they're shorter. You'd expect to have a
collision after about 10,000 "unique" numbers are generated randomly.
This isn't very many!
UUIDs can be generated using partly a number which should be unique to
each machine, plus some other factors. You could consider doing this,
giving each machine a unique prefix and generating the suffixes randomly
or sequentially. For example, if you have 100 of fewer hosts, you give
them the 2-digit prefixes 00 through 99, and let them generate IDs by
choosing the final 6 digits. If the machine does so randomly, you'll
expect a collision after about sqrt(10e6) ~ 3000 IDs per machine, and
if it is done sequentially on each machine then you can use all 10e6 IDs
on each machine. 3000 and 10e6 are both pretty small, though.
There's a reason that UUIDs are large numbers, unless your system is
guaranteed to be very small, narrow "unique numbers" will fail, and if
your system is small you might as well allocate them manually, or
automatically but sequentially from some "master" source.
Jeff
#7; Wed, 26 Dec 2007 23:36:00 GMT

- Hi,
Thanks for all your people input here, I have learnt a lot besides the
solution itself but also the way to analyze my problem here. :-)
Best regards,
-- Wong
-- Original Message --
From: "Jeff Epler" <jepler.python.itags.org.unpythonic.net>
To: "Joe Wong" <joewong.python.itags.org.mango.cc>
Cc: "Daniel 'Dang' Griffith" <noemail.python.itags.org.noemail4u.com>;
<python-list.python.itags.org.python.org>
Sent: Thursday, May 20, 2004 12:28 AM
Subject: Re: unique number generator
> This is probably not easy to do, without more requirements.
> UUIDs can be generated randomly, in which case about 120 of the 128 bits
> may vary. This means that you wouldn't expect to generate two that are
> identical before about 2^60 are generated, which is enough to make most
> people comfortable.
> 8-digit numbers, well, they're shorter. You'd expect to have a
> collision after about 10,000 "unique" numbers are generated randomly.
> This isn't very many!
> UUIDs can be generated using partly a number which should be unique to
> each machine, plus some other factors. You could consider doing this,
> giving each machine a unique prefix and generating the suffixes randomly
> or sequentially. For example, if you have 100 of fewer hosts, you give
> them the 2-digit prefixes 00 through 99, and let them generate IDs by
> choosing the final 6 digits. If the machine does so randomly, you'll
> expect a collision after about sqrt(10e6) ~ 3000 IDs per machine, and
> if it is done sequentially on each machine then you can use all 10e6 IDs
> on each machine. 3000 and 10e6 are both pretty small, though.
> There's a reason that UUIDs are large numbers, unless your system is
> guaranteed to be very small, narrow "unique numbers" will fail, and if
> your system is small you might as well allocate them manually, or
> automatically but sequentially from some "master" source.
> Jeff
#8; Wed, 26 Dec 2007 23:37:00 GMT