PHP 的 shuffle 函数有多随机?

How random is PHP#39;s shuffle function?(PHP 的 shuffle 函数有多随机?)
本文介绍了PHP 的 shuffle 函数有多随机?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

有谁知道 PHP 的 shuffle() 函数的随机性是什么?它取决于操作系统吗?它是否使用 PHP 自己的播种机?

Does anyone know what's the randomness of PHP's shuffle() function? Does it depend on the operating system? Does it use PHP's own seeder?

是否可以使用 mt_rand() 作为生成器?

Is it possible to use mt_rand() as generator?

推荐答案

shuffle() 函数基于与 rand() 相同的生成器,也就是系统基于线性同余算法的生成器.这是一个快速生成器,但或多或少具有随机性.自 PHP 4.2.0 起,随机生成器会自动播种,但您可以根据需要使用 srand() 函数来播种.

shuffle() function is based on the same generator as rand(), which is the system generator based on linear congruential algorithm. This is a fast generator, but with more or less randomness. Since PHP 4.2.0, the random generator is seeded automatically, but you can use srand() function to seed it if you want.

mtrand() 基于 Mersenne Twister 算法,其中是最好的伪随机算法之一.要使用该生成器对数组进行混洗,您需要编写自己的 shuffle 函数.例如,您可以查看 Fisher-Yates 算法.编写自己的 shuffle 函数会产生更好的随机性,但会比内置的 shuffle 函数慢.

mtrand() is based on Mersenne Twister algorithm, which is one of the best pseudo-random algorithms available. To shuffle an array using that generator, you'd need to write you own shuffle function. You can look for example at Fisher-Yates algorithm. Writing you own shuffle function will yield to better randomness, but will be slower than the builtin shuffle function.

这篇关于PHP 的 shuffle 函数有多随机?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Codeigniter htaccess to remove index.php and www(Codeigniter htaccess 删除 index.php 和 www)
htaccess mod_rewrite part of url to GET variable(htaccess mod_rewrite url 的一部分到 GET 变量)
Replacing a querystring parameter value using mod_rewrite(使用 mod_rewrite 替换查询字符串参数值)
.htaccess in subdirectory #39;overriding#39; parent htaccess(子目录“覆盖父 htaccess 中的 .htaccess)
How to rewrite SEO friendly url#39;s like stackoverflow(如何像stackoverflow一样重写SEO友好的url)
Is it okay to have a very long .htaccess file?(有一个很长的 .htaccess 文件可以吗?)