str_shuffle 和随机性
不久前我写了一个随机字符串生成器,它使用字符串中的第 mt_rand() 个字符构建一个字符串,直到达到所需的长度.
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)
今天,在完全研究其他东西时,我偶然发现了以下代码:
Today, when researching something else entirely I stumbled upon the following code:
function generateRandomString ($length = 10) {
return substr(str_shuffle ("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, $length);
}
这与我在一行中循环基于 mt_rand() 的代码实现的效果几乎相同.我真的很喜欢它,原因很简单,更少的代码行总是一件好事.:)
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. :)
但是当我在 PHP 手册中查找 str_shuffle 时,它的文档非常简单.我非常想学习的一件事是它使用什么算法来实现随机性?手册没有提到进行什么样的随机化来获得洗牌的字符串.如果它使用 rand() 而不是 mt_rand() 那么坚持我当前的解决方案可能会更好.
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()?我正在使用我的随机字符串函数来生成密码,所以随机性的质量很重要.
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:正如已经指出的那样,str_shuffle 方法不等同于我已经在使用的代码,并且由于字符串的字符与输入保持相同,因此随机性会降低,仅他们的顺序改变了.但是,我仍然很好奇 str_shuffle 函数如何随机化其输入字符串.
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.
正如已经指出的那样,str_shuffle 方法不等同于我已经在使用的代码,并且由于字符串的字符与输入保持相同,只是它们的顺序发生了变化,因此随机性会降低.但是我仍然很好奇 str_shuffle 函数如何随机化其输入字符串.
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
并查看每个函数的可视化表示
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()?我正在使用我的随机字符串函数来生成密码,所以随机性的质量很重要.
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
产生与 rand
几乎相同的输出......
You can see clearly that str_shuffle
produces almost same output as rand
...
相关文章