unique number generator

Robert Brewer fumanchu at amor.org
Wed May 19 10:48:03 EDT 2004


Joe Wong wrote:
> 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? :-)

If you need to write your own, the core of it can just be a generator:

def _unique(seed):
    i = 99999999
    while i:
        yield seed % i
        i -= 1

id_gen() = _unique("")
id_gen.next()
id_gen.next()
id_gen.next()

...then you have several mechanisms to share those numbers with consumer
code, whether shared mem, lockfiles, sockets, whatever. You just need to
ensure that the generator continues to run, because if you restart that
process, you repeat numbers. For some apps, that might be OK. If it's
not OK, modify the generator to save state somehow (shelve comes to
mind) and pick up where it left off on restart.


Robert Brewer
MIS
Amor Ministries
fumanchu at amor.org




More information about the Python-list mailing list