久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

str_shuffle 和隨機性

str_shuffle and randomness(str_shuffle 和隨機性)
本文介紹了str_shuffle 和隨機性的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

不久前我寫了一個隨機字符串生成器,它使用字符串中的第 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)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Action View Helper in Zend - Work around?(Zend 中的動作視圖助手 - 解決方法?)
Is this a good way to match URI to class/method in PHP for MVC(這是將 URI 與 PHP 中用于 MVC 的類/方法匹配的好方法嗎)
Where do I save partial (views) in Zend Framework, to be accessible for all Views in my App?(我在哪里保存 Zend Framework 中的部分(視圖),以便我的應(yīng)用程序中的所有視圖都可以訪問?) - IT屋-程序員軟件開發(fā)技術(shù)
Having a single entry point to a website. Bad? Good? Non-issue?(有一個網(wǎng)站的單一入口點.壞的?好的?沒問題?)
Is MVC + Service Layer common in zend or PHP?(MVC + 服務(wù)層在 Zend 或 PHP 中常見嗎?)
Hello World example in MVC approach to PHP(PHP MVC 方法中的 Hello World 示例)
主站蜘蛛池模板: 免费在线看黄 | 一级欧美 | 色欧美片视频在线观看 | 亚洲狠狠爱| 国产成人精品区一区二区不卡 | 亚洲欧美综合精品久久成人 | 一区二区在线不卡 | av大片 | 日韩欧美国产精品一区二区三区 | 国产精品色 | 日韩二三区 | 亚洲精品视频免费观看 | 日本免费黄色 | 亚洲一区三区在线观看 | 欧美一级精品片在线看 | 亚洲综合二区 | 久久九九99 | 国产精品免费一区二区三区四区 | www.99re | 在线日韩中文字幕 | 亚洲成人动漫在线观看 | 欧美一区二区三区久久精品视 | 久久久久综合 | 久久中文一区二区 | 亚洲精品九九 | 亚洲免费精品 | 毛片视频免费观看 | 久草免费视| 精品综合久久久 | 国产精品一区二区在线 | 999久久久久久久久 国产欧美在线观看 | 国产精品久久99 | 成人欧美一区二区三区黑人孕妇 | 99精品网 | 亚洲精品麻豆 | 99热视| 天天干天天玩天天操 | 黑人巨大精品欧美一区二区免费 | 韩国av网站在线观看 | 奇米在线| 亚洲一区二区免费看 |