如何生成只有7个字符的UID并排除重复,如imgur.com
take a look at this URL: http://imgur.com/JLhIuYt
You will see that the URL has a seemingly random string generated, which is build from 7 characters of
- small letters (26 characters)
- big letters (26 characters)
- numbers (10 numbers)
n = (26+26+10) = 62
I would like to know how it is possible to generate a random string of only 7 characters, that works as a GUID.
With only 7 characters, imgur is using, they can generate 3.521.614.606.208 variations (62 to the power of 7). The question now arises, how imgur handles each variation to be used as an identifier, since it seems that those numbers are generated randomly.
Is there a way to find out, how it is possible to use 7 characters as UID and making sure, they don't repeat themselves?
One solution could be, to generate them in chunks and to use one after the other. Seems rather not good.
Any kind appreciated!
Btw. Random Strin best generated in PHP
看一下这个网址: http://imgur.com/JLhIuYt p>
您将看到URL生成了一个看似随机的字符串,该字符串由7个字符构成 p>
- 小写字母(26个字符) li>
- 大字母(26个字符) li>
- 数字(10个数字)
ol>
n =(26 + 26 + 10)= 62 p>
我想知道如何生成一个 只有7个字符的随机字符串,用作GUID。 p>
只有7个字符,imgur正在使用,它们可以生成3.521.614.606.208变量(62到7的幂) 。 现在出现的问题是,imgur如何处理每个变体用作标识符,因为这些数字似乎是随机生成的。 p>
有没有办法找出,怎么可能 使用7个字符作为UID并确保它们不会重复? p>
一个解决方案可能是,以块的形式生成它们并一个接一个地使用它们。 似乎并不好。 p>
任何一种赞赏! p>
顺便说一下。 Random Strin最好用PHP生成 p> div>
You can use this one-liner to generate a random string:
substr(
str_shuffle(
str_repeat('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890', 7)
),
0,
7
)
Then, you can check in a database if it's already used or not like this for example:
while(
existsInTheDB(
$str = substr(
str_shuffle(
str_repeat(
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890',
7
)
),
0,
7
)
)
){}
//now you can use $str
I assume they have a database in the backend and handle the collisions. You generate an identifier, look if it still exists, then generate another one till you get one that doesn't exist.
Or they have just incremented some 64bit integer and generated a string from it.