3 ms·
Ask HN: how can I generate youtube style id?
Given sample URL: http://www.youtube.com/watch?v=xqvObyU2ITs, any availabale algorithm to generate youtube style video id (xqvObyU2ITs)? Thanks!
-Jack
- inklesspen 18y agouh, 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.
- staunch 18y agoFirst thought is that they're generating a globally unique video ID number and base-62 encoding it to keep the URL shorter. Maybe not though? You could do that easily by just base-62 encoding your table's auto-generated primary key.
- jcapote 18y agodef generate_code(len = 5) (1..len).map { (("a".."z").to_a + ("A".."Z").to_a + (0..9).to_a)[rand(62)] }.join end
- wooby 18y agoThat rocks dude.
- compay 18y agoHere's a very quick and dirty one for Ruby: require '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.
- HeyLaughingBoy 18y agoWhat's wrong with starting with '0' for the first item and incrementing up from there? Guaranteed uniqueness.
- throw_away 18y agoa) impossible to do in a distributed fashion, b) may leak information about your operations to competitors. of course, OP may not care about these things.
- HeyLaughingBoy 18y agoI understand (b), and in fact I considered it a potential weakness, but why does (a) matter if you're selling from a single site? Even if you got multiple simultaneous orders, storing the last license generated in a DBMS should make it multiprocess-safe.
- wooby 18y agoOr, using C with /dev/urandom: http://alan.dipert.org/post/84526522/random-strings-with-c http://alan.dipert.org/post/84526522/random-strings-with-c