By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,923 Members | 1,443 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,923 IT Pros & Developers. It's quick & easy.

unique number generator

P: n/a
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

Jul 18 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
On Wed, 19 May 2004 18:36:40 +0800, "Joe Wong" <jo*****@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
Jul 18 '05 #2

P: n/a
"Joe Wong" <jo*****@mango.cc> wrote in message news:<ma************************************@pytho n.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/
Jul 18 '05 #3

P: n/a
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" <no*****@noemail4u.com>
Newsgroups: comp.lang.python
To: <py*********@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" <jo*****@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

Jul 18 '05 #4

P: n/a
Daniel 'Dang' Griffith <no*****@noemail4u.com> writes:
On Wed, 19 May 2004 18:36:40 +0800, "Joe Wong" <jo*****@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.

--
ha************@boeing.com
6-6M21 BCA CompArch Design Engineering
Phone: (425) 342-0007
Jul 18 '05 #5

P: n/a
"Joe Wong" <jo*****@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.
--
ha************@boeing.com
6-6M21 BCA CompArch Design Engineering
Phone: (425) 342-0007
Jul 18 '05 #6

P: n/a
"Joe Wong" <jo*****@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.
Jul 18 '05 #7

P: n/a
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

Jul 18 '05 #8

P: n/a
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" <je****@unpythonic.net>
To: "Joe Wong" <jo*****@mango.cc>
Cc: "Daniel 'Dang' Griffith" <no*****@noemail4u.com>;
<py*********@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

Jul 18 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.