mt_rand的缺点是什么? [英] What's the disadvantage of mt_rand?

查看:131
本文介绍了mt_rand的缺点是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

偏见的定义是什么

mt_rand()返回值的分布偏向于64位的偶数max超过2 ^ 32时会生成PHP版本.

The distribution of mt_rand() return values is biased towards even numbers on 64-bit builds of PHP when max is beyond 2^32.

如果这是替代四舍五入规则的一种偏见,我不会认为这确实很重要(因为这种偏见并不明显).

If it's the kind of bias stated in alternate tie-breaking rules for rounding, I don't think it really matters (since the bias is not really visible).

除了mt_rand()声称要比rand()快四倍,通过在前面添加三个字符!

Besides mt_rand() is claimed to be four times faster than rand(), just by adding three chars in front!

假设mt_rand可用,那么使用它的缺点是什么?

Assuming mt_rand is available, what's the disadvantage of using it?

推荐答案

mt_rand使用 Merenne Twister 算法,它比rand通常使用的 LCG 更好.例如,LCG的周期仅为2 32 ,而mt_rand的周期为2 19937 −1.此外,LCG生成的所有值都将在线上躺下或平面绘制到多维空间中.而且,不仅在实践上可行,而且相对容易地确定LCG的参数. LCG的唯一优势可能是速度稍快,但在使用php进行编码时,规模是完全不相关的.

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 也不适合用于加密目的(生成令牌,密码或加密密钥).

However, mt_rand is not suitable for cryptographic purposes (generation of tokens, passwords or cryptographic keys) either.

如果需要加密随机性,请在php 7中使用 random_int .在较旧的php版本中,请从<符合POSIX的操作系统上为c7>或/dev/random.

If you need cryptographic randomness, use random_int in php 7. On older php versions, read from /dev/urandom or /dev/random on a POSIX-conforming operating system.

这篇关于mt_rand的缺点是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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