str_shuffle和随机性 [英] str_shuffle and randomness

查看:86
本文介绍了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.

更新:正如已经指出的那样,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,它使用

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 ...

这篇关于str_shuffle和随机性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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