TinyURL的风格独特的code:潜在的算法,prevent碰撞 [英] Tinyurl-style unique code: potential algorithm to prevent collisions

查看:383
本文介绍了TinyURL的风格独特的code:潜在的算法,prevent碰撞的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个系统,需要一个唯一的6位code重新present一个对象,我试图想一个好的算法生成他们。下面是pre-请求数:

I have a system that requires a unique 6-digit code to represent an object, and I'm trying to think of a good algorithm for generating them. Here are the pre-reqs:

  • 在我使用的一个基地 - 20系统(没有上限,数字,元音,或l至prevent混乱和下流话)
    • 基-20可允许64万种组合
    • I'm using a base-20 system (no caps, numbers, vowels, or l to prevent confusion and naughty words)
      • The base-20 allows 64 million combinations

      我听起来像它会工作的想法,但我不是在数学不够好,弄清楚如何实现它:如果我从0开始,增量N,则转换为基础-20,似乎就像应该有N个部分的价值,让我重复之前的任何指望从0-63,999,999每个值。

      I had an idea that sounded like it would work, but I'm not good enough at math to figure out how to implement it: if I start at 0 and increment by N, then convert to base-20, it seems like there should be some value for N that lets me count each value from 0-63,999,999 before repeating any.

      例如,从0到9的使用,N = 3(10如此模3)去:0,3,6,9,2,5,8,1,4,7

      For example, going from 0 through 9 using N=3 (so 10 mod 3): 0, 3, 6, 9, 2, 5, 8, 1, 4, 7.

      有一些神奇的数学方法计算出的N值一些较大的数字,能够在整个范围内来算不重复?理想情况下,我选择了一些排序将围绕一套这样的,这是不是很明显,有一个图案跳跃的,但我不知道如何可能是。

      Is there some magic math method for figuring out values of N for some larger number that is able to count through the whole range without repeating? Ideally, the number I choose would sort of jump around the set such that it wasn't obvious that there was a pattern, but I'm not sure how possible that is.

      另外,散列算法,保证值0-64万美元将工作的独特性,但我的方式太愚蠢知道这是可能的。

      Alternatively, a hashing algorithm that guaranteed uniqueness for values 0-64 million would work, but I'm way too dumb to know if that's possible.

      推荐答案

      所有你需要的是一个数字,没有共享的因素与你的密钥空间。最简单数值是使用一个质数。你可以谷歌的大素数,或使用<一个href="http://primes.utm.edu/lists/small/10000.txt">http://primes.utm.edu/lists/small/10000.txt

      All you need is a number that shares no factors with your key space. Easiest value is to use a prime number. You can google for large primes, or use http://primes.utm.edu/lists/small/10000.txt

      这篇关于TinyURL的风格独特的code:潜在的算法,prevent碰撞的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆