9 ms·
uh, in python: import string, random ''.join(random.sample((string.letters+string.digits), 12)) I hope that's what you were asking for. If not, you might
by inklesspen 18y ago
uh, in python:
import string, random
''.join(random.sample((string.letters+string.digits), 12))
I hope that's what you were asking for. If not, you might want to clarify your question.
- staunch 18y agoYou want to use an ID that can predictably be unique for something like this. You shouldn't use a random string.
- aristus 18y agoWhat do you mean by "predictably unique"? Do you mean "guaranteed unique"? As a way to generate unique ids this isn't horrible. 12^62 is something like 220 bits. The odds of a collision are even lower than with a UUID. Guaranteed uniqueness is preferred, yes. But the level of effort needed to guarantee uniqueness across a large application / dataset / etc is much higher than "unique enough", just as it's a lot more expensive to prove a number is prime than to generate a number that is 99.9999% probably prime.
- jacktang 18y agowell, even 99.9999% possible, we should handle the 0.0001% exception ;) Can I understand that, if collision occurs, let the it generate the id again?
- jacktang 18y agoHi, how to keep the string/id is unique? In another words, how to deal with the id conflict?
- aristus 18y agoIt's generating a big (BIG) random numbe. The odds of a conflict are many billion times more than the odds of getting hit by a meteorite. http://en.wikipedia.org/wiki/UUID#Random_UUID_probability_of_duplicates http://en.wikipedia.org/wiki/UUID#Random_UUID_probability_of...
- aristus 18y agoOn morning's light, that won't do what you think. random.sample() gives you a unique sampling. No character will be repeated. try this instead: alphanum = string.letters+string.digits ''.join([alphanum[random.randint(0,61)] for i in xrange(12)])
- inklesspen 18y agoYou're quite right; I should have caught that.