mt_rand 的缺点是什么?

2023-11-29

偏见的定义是什么:

的分布mt_rand()当 max 超过 2^32 时,返回值在 64 位 PHP 版本上偏向于偶数。

如果这是其中所述的偏见舍入的替代平局打破规则,我认为这并不重要(因为偏见并不真正可见)。

Besides mt_rand() is claimedrand(),只需在前面添加三个字符即可!

假设mt_rand可以使用,使用它有什么缺点?


mt_rand uses the Mersenne Twister algorithm, which is far better than the LCG typically used by rand. For example, the period of an LCG is a measly 232, whereas the period of mt_rand is 219937 − 1. Also, all the values generated by an LCG will lie on lines or planes when plotted into a multidimensional space. Also, it is not only practically feasible, but relatively easy to determine the parameters of an LCG. The only advantage LCGs have is being potentially slightly faster, but on a scale that is completely irrelevant when coding in php.

然而,mt_rand is 不适合加密目的(生成令牌、密码或加密密钥)。

如果您需要加密随机性,请使用random_int在 php 7 中。在较旧的 php 版本上,读取/dev/urandom or /dev/random在符合 POSIX 的操作系统上。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

mt_rand 的缺点是什么? 的相关文章

随机推荐