TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Ask HN: how can I generate youtube style id?

4 pointsby jacktangover 16 years ago
Given sample URL: http://www.youtube.com/watch?v=xqvObyU2ITs, any availabale algorithm to generate youtube style video id (xqvObyU2ITs)? Thanks!<p>-Jack

6 comments

jcapoteover 16 years ago
def generate_code(len = 5)<p><pre><code> (1..len).map { (("a".."z").to_a + ("A".."Z").to_a + (0..9).to_a)[rand(62)] }.join </code></pre> end
评论 #507848 未加载
compayover 16 years ago
Here's a very quick and dirty one for Ruby:<p>require 'zlib'<p>base = "whatever"<p>salt = "whatever"<p>Zlib.crc32(base + salt).to_s(36)<p>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.
staunchover 16 years ago
First 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?<p>You could do that easily by just base-62 encoding your table's auto-generated primary key.
woobyabout 16 years ago
Or, using C with /dev/urandom: <a href="http://alan.dipert.org/post/84526522/random-strings-with-c" rel="nofollow">http://alan.dipert.org/post/84526522/random-strings-with-c</a>
HeyLaughingBoyover 16 years ago
What's wrong with starting with '0' for the first item and incrementing up from there? Guaranteed uniqueness.
评论 #486409 未加载
inklesspenover 16 years ago
uh, in python:<p><pre><code> import string, random ''.join(random.sample((string.letters+string.digits), 12)) </code></pre> I hope that's what you were asking for. If not, you might want to clarify your question.
评论 #486022 未加载
评论 #485452 未加载
评论 #485495 未加载