TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Ask HN: how can I generate youtube style id?

4 点作者 jacktang超过 16 年前
Given sample URL: http://www.youtube.com/watch?v=xqvObyU2ITs, any availabale algorithm to generate youtube style video id (xqvObyU2ITs)? Thanks!<p>-Jack

6 条评论

jcapote超过 16 年前
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 未加载
compay超过 16 年前
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.
staunch超过 16 年前
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.
wooby大约 16 年前
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>
HeyLaughingBoy超过 16 年前
What's wrong with starting with '0' for the first item and incrementing up from there? Guaranteed uniqueness.
评论 #486409 未加载
inklesspen超过 16 年前
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 未加载