mt_rand 的缺点是什么?

What#39;s the disadvantage of mt_rand?(mt_rand 的缺点是什么?)
本文介绍了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 使用 Mersenne Twister 算法,远优于 LCGLCG代码>rand.例如,LCG 的周期是微不足道的 232,而 mt_rand 的周期是 219937 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 的操作系统上从 /dev/urandom/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 的缺点是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)