Ask HN: how can I generate youtube style id?
-Jack
-Jack
(1..len).map { (("a".."z").to_a + ("A".."Z").to_a + (0..9).to_a)[rand(62)] }.join
endrequire 'zlib'
base = "whatever"
salt = "whatever"
Zlib.crc32(base + salt).to_s(36)
This will generate 6-7 character strings. Not sure how likely collisions are, but they should be rare enough that a simple check/regenerate should work.
You could do that easily by just base-62 encoding your table's auto-generated primary key.
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. alphanum = string.letters+string.digits
''.join([alphanum[random.randint(0,61)] for i in xrange(12)])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.
http://en.wikipedia.org/wiki/UUID#Random_UUID_probability_of...