PHP 的 shuffle 函数有多随机?
有谁知道PHP的shuffle()
函数的随机性是什么?它取决于操作系统吗? 它使用 PHP 自己的播种器吗?
是否可以使用 mt_rand() 作为生成器?
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?
Is it possible to use mt_rand()
as generator?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
shuffle()
函数与rand()
基于相同的生成器,它是基于 线性同余算法。这是一个快速生成器,但或多或少具有随机性。从 PHP 4.2.0 开始,随机生成器会自动播种,但如果需要,您可以使用 srand() 函数来播种。mtrand()
基于Mersenne Twister 算法,它是以下之一可用的最佳伪随机算法。要使用该生成器对数组进行洗牌,您需要编写自己的洗牌函数。例如,您可以查看 Fisher-Yates 算法。编写自己的 shuffle 函数将产生更好的随机性,但会比内置 shuffle 函数慢。shuffle()
function is based on the same generator asrand()
, 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 usesrand()
function to seed it if you want.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 7.1 更新
由于 PHP 7.1 实现了 rng_fixes rfc,因此
shuffle 的实现
现在使用 Mersenne Twister PRNG(即它使用mt_rand
并受到调用mt_srand
的影响)。旧系统 PRNG (
rand
) 不再可用;函数rand
和srand
实际上是它们的mt_
等效函数的别名。Update for PHP 7.1
Since the rng_fixes rfc was implemented for PHP 7.1, the implementation of
shuffle
now utilizes the Mersenne Twister PRNG (i.e. it usesmt_rand
and is affected by callingmt_srand
).The legacy system PRNG (
rand
) is no longer available; the functionsrand
andsrand
are in fact aliased to theirmt_
equivalents.根据 Mirouf 的回答(非常感谢您的贡献)...我对其进行了一些改进,以消除冗余的数组计数。为了我自己的理解,我还对变量进行了稍微不同的命名。
如果你想像 shuffle() 一样使用它,你可以修改要通过引用传递的参数,即 &$array,然后确保将 return 更改为简单的:“return;”并将生成的随机数组分配回 $array,如下所示:
$array = $randArr; (返回之前)。
Based on Mirouf's answer (thank you so much for your contribution)... I refined it a little bit to take out redundant array counting. I also named the variables a little differently for my own understanding.
If you want to use this exactly like shuffle(), you could modify the parameter to be passed by reference, i.e. &$array, then make sure you change the return to simply: "return;" and assign the resulting random array back to $array as such:
$array = $randArr; (Before the return).
就像
rand()
一样,它是随机的;作为 PHP 风格,你不需要播种
It's random just like
rand()
;And as PHP style you don't need to seed
生成一个随机数。
随机化数组。它还在数组中生成新的键,而不仅仅是重新排列旧的键。
如果您想在 PHP 中播种,您可以使用
mt_strand()
。但是,由于 PHP 4.2.0,当您调用 mt_rand 时,PHP 中会自动完成播种。
Generates a random number.
Randomizes an array. It also generates new keys in the array rather than just rearranging the old ones.
If you want to seed in PHP you would have used
mt_strand()
.However, since PHP 4.2.0 seeding is done automatically in PHP when you call mt_rand.
适用于关联数组和数值数组:
Works with associative and numeric arrays:
我创建了一个对数组进行随机排序的函数。
这不是最好的方法,但是当我使用函数 shuffle 时,它总是以相同的顺序返回随机数组。如果这可以帮助某人,我会很高兴!
I've created a function who sort my array randomly.
It's not the best way to do that but when I've used the function shuffle, it was always returning a random array in the same order. If this could help someone, I will be happy !