問題描述
不久前我寫了一個隨機字符串生成器,它使用字符串中的第 mt_rand() 個字符構(gòu)建一個字符串,直到達到所需的長度.
A while back I wrote a random string generator that builds a string using the mt_rand()th character in a string until the desired length is reached.
public function getPassword ()
{
if ($this -> password == '')
{
$pw = '';
$charListEnd = strlen (static::CHARLIST) - 1;
for ($loops = mt_rand ($this -> min, $this -> max); $loops > 0; $loops--)
{
$pw .= substr (static::CHARLIST, mt_rand (0, $charListEnd), 1);
}
$this -> password = $pw;
}
return $this -> password;
}
(CHARLIST 是一個包含密碼字符池的類常量.$min 和 $max 是長度限制)
(CHARLIST is a class constant containing a pool of characters for the password. $min and $max are length contraints)
今天,在完全研究其他東西時,我偶然發(fā)現(xiàn)了以下代碼:
Today, when researching something else entirely I stumbled upon the following code:
function generateRandomString ($length = 10) {
return substr(str_shuffle ("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, $length);
}
這與我在一行中循環(huán)基于 mt_rand() 的代碼實現(xiàn)的效果幾乎相同.我真的很喜歡它,原因很簡單,更少的代碼行總是一件好事.:)
This accomplishes pretty much the same effect as my looping mt_rand() based code in one line. I really like it for that simple reason, fewer lines of code is always a good thing. :)
但是當(dāng)我在 PHP 手冊中查找 str_shuffle 時,它??的文檔非常簡單.我非常想學(xué)習(xí)的一件事是它使用什么算法來實現(xiàn)隨機性?手冊沒有提到進行什么樣的隨機化來獲得洗牌的字符串.如果它使用 rand() 而不是 mt_rand() 那么堅持我當(dāng)前的解決方案可能會更好.
But when I looked up str_shuffle in PHP's manual the documentation on it was pretty light. One thing I was really keen to learn was what algorithm does it use for randomness? The manual doesn't mention what kind of randomization is done to get the shuffled string. If it uses rand() instead of mt_rand() then sticking to my current solution may be better after all.
所以基本上我想知道 str_shuffle 如何隨機化字符串.它使用的是 rand() 還是 mt_rand()?我正在使用我的隨機字符串函數(shù)來生成密碼,所以隨機性的質(zhì)量很重要.
So basically I'd like to know how str_shuffle randomizes the string. Is it using rand() or mt_rand()? I'm using my random string function to generate passwords, so the quality of the randomness matters.
UPDATE:正如已經(jīng)指出的那樣,str_shuffle 方法不等同于我已經(jīng)在使用的代碼,并且由于字符串的字符與輸入保持相同,因此隨機性會降低,僅他們的順序改變了.但是,我仍然很好奇 str_shuffle 函數(shù)如何隨機化其輸入字符串.
UPDATE: As has been pointed out, the str_shuffle method is not equivalent to the code I'm already using and will be less random due to the string's characters remaining the same as the input, only with their order changed. However I'm still curious as to how the str_shuffle function randomizes its input string.
推薦答案
更好的解決方案是 mt_rand
使用 Mersenne Twister 哪個更好.
A better solution would be mt_rand
which uses Mersenne Twister which much more better.
正如已經(jīng)指出的那樣,str_shuffle 方法不等同于我已經(jīng)在使用的代碼,并且由于字符串的字符與輸入保持相同,只是它們的順序發(fā)生了變化,因此隨機性會降低.但是我仍然很好奇 str_shuffle 函數(shù)如何隨機化其輸入字符串.
As has been pointed out, the str_shuffle method is not equivalent to the code I'm already using and will be less random due to the string's characters remaining the same as the input, only with their order changed. However I'm still curious as to how the str_shuffle function randomizes its input string.
為了使輸出相等,我們只需使用 0,1
并查看每個函數(shù)的可視化表示
To make the output equal lets just use 0,1
and look at the visual representation of each of the functions
簡單的測試代碼
header("Content-type: image/png");
$im = imagecreatetruecolor(512, 512) or die("Cannot Initialize new GD image stream");
$white = imagecolorallocate($im, 255, 255, 255);
for($y = 0; $y < 512; $y ++) {
for($x = 0; $x < 512; $x ++) {
if (testMTRand()) { //change each function here
imagesetpixel($im, $x, $y, $white);
}
}
}
imagepng($im);
imagedestroy($im);
function testMTRand() {
return mt_rand(0, 1);
}
function testRand() {
return rand(0, 1);
}
function testShuffle() {
return substr(str_shuffle("01"), 0, 1);
}
輸出 testRand()
輸出 testShuffle()
輸出 testMTRand()
所以基本上我想知道 str_shuffle 如何隨機化字符串.它使用的是 rand() 還是 mt_rand()?我正在使用我的隨機字符串函數(shù)來生成密碼,所以隨機性的質(zhì)量很重要.
So basically I'd like to know how str_shuffle randomizes the string. Is it using rand() or mt_rand()? I'm using my random string function to generate passwords, so the quality of the randomness matters.
你可以清楚地看到 str_shuffle
產(chǎn)生與 rand
幾乎相同的輸出......
You can see clearly that str_shuffle
produces almost same output as rand
...
這篇關(guān)于str_shuffle 和隨機性的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!