unique number generator

Discussion in 'Python' started by Joe Wong, May 19, 2004.

  1. Joe Wong

    Joe Wong Guest

    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
     
    Joe Wong, May 19, 2004
    #1
    1. Advertising

  2. On Wed, 19 May 2004 18:36:40 +0800, "Joe Wong" <>
    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
     
    Daniel 'Dang' Griffith, May 19, 2004
    #2
    1. Advertising

  3. Joe Wong

    Joe Wong Guest

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


    > On Wed, 19 May 2004 18:36:40 +0800, "Joe Wong" <>
    > 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
    >
     
    Joe Wong, May 19, 2004
    #3
  4. Joe Wong

    george young Guest

    george young, May 19, 2004
    #4
  5. Joe Wong

    Harry George Guest

    Daniel 'Dang' Griffith <> writes:

    > On Wed, 19 May 2004 18:36:40 +0800, "Joe Wong" <>
    > 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.

    --

    6-6M21 BCA CompArch Design Engineering
    Phone: (425) 342-0007
     
    Harry George, May 19, 2004
    #5
  6. Joe Wong

    Harry George Guest

    "Joe Wong" <> 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.


    --

    6-6M21 BCA CompArch Design Engineering
    Phone: (425) 342-0007
     
    Harry George, May 19, 2004
    #6
  7. Joe Wong

    Jeff Epler Guest

    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
     
    Jeff Epler, May 19, 2004
    #7
  8. Joe Wong

    Nelson Minar Guest

    "Joe Wong" <> 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.
     
    Nelson Minar, May 19, 2004
    #8
  9. Joe Wong

    Joe Wong Guest

    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" <>
    To: "Joe Wong" <>
    Cc: "Daniel 'Dang' Griffith" <>;
    <>
    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
    >
     
    Joe Wong, May 20, 2004
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Martin Maurer
    Replies:
    3
    Views:
    5,023
    Peter
    Apr 19, 2006
  2. ToshiBoy
    Replies:
    6
    Views:
    877
    ToshiBoy
    Aug 12, 2008
  3. Krist
    Replies:
    11
    Views:
    3,570
    steph
    Dec 7, 2010
  4. Thomas Hafner

    global generator of unique symbols

    Thomas Hafner, Jan 31, 2009, in forum: Ruby
    Replies:
    8
    Views:
    226
    Matthew Moss
    Jan 31, 2009
  5. Token Type
    Replies:
    9
    Views:
    385
    Chris Angelico
    Sep 9, 2012
Loading...

Share This Page