unique number generator

J

Joe Wong

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
 
D

Daniel 'Dang' Griffith

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
 
J

Joe Wong

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" <[email protected]>
Newsgroups: comp.lang.python
To: <[email protected]>
Sent: Wednesday, May 19, 2004 9:06 PM
Subject: Re: unique number generator

on same or different machines will make use of it. Is there any library /
project available already for this?
 
H

Harry George

Daniel 'Dang' Griffith said:
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.
 
H

Harry George

Joe Wong said:
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.
 
J

Jeff Epler

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
 
N

Nelson Minar

Joe Wong said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top