You are not logged in.

#1 2007-04-07 00:33:48

Simastrick
Member
Registered: 2006-08-17
Posts: 49

generate random unique IDs in python?

I have an example scenario and a question:

I have a database of several thousand items, each of which has an ITEM_ID (00000001, 00000002, etc...). However, I would like end users to see a unique and seemingly random ID for each item (so that a user will always see item# "48D3L912" instead of "00000001", for instance).

I'm trying to do this in python, and I'm thinking that these ID's would just be dynamically generated from the ITEM_IDs every time a user looks at his items. I would also like the "random" ID to have the same length as the ITEM_ID. Is there an easy way to do this?

I've encountered python's hash() function although the hash length seems to be unchangeable. I guess I could also think up my own silly algorithm for this, but I hope there's an easier and better solution..

P.S. I'm a python/programming rookie so please be nice.

Last edited by Simastrick (2007-04-07 00:34:46)

Offline

#2 2007-04-07 00:47:37

Simastrick
Member
Registered: 2006-08-17
Posts: 49

Re: generate random unique IDs in python?

I forgot to say that I also want to avoid simply generating random numbers for each item and then checking the database to see if the generated number has already been used, because that seems inefficient.

Perhaps a hash function that can create 8-character hashes would be ideal..

Last edited by Simastrick (2007-04-07 00:50:59)

Offline

#3 2007-04-07 05:15:43

jonathantan86
Member
Registered: 2004-05-02
Posts: 42

Re: generate random unique IDs in python?

I think you need a 1-1 correspondence function (bijection). Take a look at this (look for generators with a full period):

http://en.wikipedia.org/wiki/Linear_con … _generator

Be careful of hash functions though...there might be collisions.

If it really is important to you, simpler would be to generate the ID yourself instead of using AUTO_INCREMENT (random number between 1 and 2 billion, say) and check if the ID already exists in the database (your database should be indexed by ID, since it should be a primary key). If it is, increment ID and try again.

Offline

#4 2007-04-07 14:18:48

deficite
Member
From: Augusta, GA
Registered: 2005-06-02
Posts: 693

Re: generate random unique IDs in python?

The random number generator should never repeat 

Python Documentation wrote:

The period [of the random number generator] is 2**19937-1

The random module has a function called "random.getstate()". Whenever you exit your program (or every time you make a random number if you are scared of python crashing, although this would be even more inefficient than what you described) you could pickle the results of this and whenever you start the program again you'd unpickle it and call "random.setstate()" on it (look up the pickle class). Another way would be to pick a seed value and store it (either in a file or hardcoded) and store how many iterations you've ran and then whenever you start the program again, it'd seed itself and then run the amount of iterations it needs to bring you back. Either this or if you know what the last value used is, you could seed to the known seed and then keep looping it until you get the same result. I personally would use the pickling method if I didn't want to check if the random number is used already.

Last edited by deficite (2007-04-07 14:22:27)

Offline

#5 2007-05-09 23:59:13

lietuva
Banned
Registered: 2005-09-30
Posts: 36

Re: generate random unique IDs in python?

You may also be interested in a maximal length linear feedback shift register (LFSR).  Details here:

http://en.wikipedia.org/wiki/Linear_fee … t_register

http://homepage.mac.com/afj/lfsr.html

Last edited by lietuva (2007-05-10 00:00:05)


The password to this account is lietuvis

Offline

#6 2007-05-10 00:54:46

iphitus
Forum Fellow
From: Melbourne, Australia
Registered: 2004-10-09
Posts: 4,927

Re: generate random unique IDs in python?

or save yourself the trouble and just go from 0000001 to wherever?

why the unnecessary complexity?

James

Offline

#7 2007-05-10 14:15:07

Galdona
Member
Registered: 2006-03-15
Posts: 196

Re: generate random unique IDs in python?

Oh, I've forgotten about this thread. Thanks for your answers everyone.

I decided to just do a simple mapping thing, where each digit maps to one of several possible characters, depending on some rules. Of course, in the database it's just an auto_increment column (00000001, etc..).

The 'random' IDs are for the 'benefit' of the users.

iphitus wrote:

or save yourself the trouble and just go from 0000001 to wherever?

why the unnecessary complexity?

James

Because I have a reason.
And that reason is not necessarily unnecessary, especially if you don't know what it is. tongue

Last edited by Galdona (2007-05-10 14:16:15)

Offline

Board footer

Powered by FluxBB