試試這個:
// generate random ID
function generateRandomID ($len) {
//To Pull Unique Random Values Out Of AlphaNumeric
//removed number 0, capital o, number 1 and small L
//Total: keys = 32, elements = 33
$characters = array(
"A","B","C","D","E","F","G","H","J","K","L","M",
"N","P","Q","R","S","T","U","V","W","X","Y","Z",
"1","2","3","4","5","6","7","8","9");
//make an "empty container" or array for our keys
$keys = array();
//first count of $keys is empty so "1", remaining count is 1-6 = total 7 times
while(count($keys) < $len) {
//"0" because we use this to FIND ARRAY KEYS which has a 0 value
//"-1" because were only concerned of number of keys which is 32 not 33
//count($characters) = 33
$x = mt_rand(0, count($characters)-1);
if(!in_array($x, $keys)) {
$keys[] = $x;
}
}
foreach($keys as $key){
$random_chars .= $characters[$key];
}
return $random_chars;
}
編輯:刪除一些字母和數字爲更好的方便用戶的閱讀,請在代碼中的註釋。
當您談論'密碼'時,我假設您希望能夠再次解密,對吧?你不是在尋找一個加密哈希函數?這在security.stackexchange中可能會更好。 – Joost
首先,我想到了建議[Vigenènere密碼](http://en.wikipedia.org/wiki/Vigen%C3%A8re_cipher),但看到你的明文(數字從1到50k)是如此有限(和有序,在那)它變成一個弱caeser cypher ..你能發佈一些更多的要求? – Joost
Joost是對的我希望能夠解密代碼。 – wildnove