如何在PHP中生成超过32位限制的随机数? [英] How do you generate a random number over the 32bit limit in PHP?

查看:475
本文介绍了如何在PHP中生成超过32位限制的随机数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在其他语言中,我会部分生成它并将其存储为字符串,但是使用PHP时,您无法选择数据类型,因此该类型不起作用,它会自动设置为整数,因此最多只能存储最大数量整数大小.

In other languages I would generate it in parts and store it in a string but with PHP you can't choose the data type so that doesn't work, its automatically set as an integer then can only store up to the maximum integer size.

对此有什么解决办法吗?

Is there any solution to this?

推荐答案

是的,您可以获取一些随机数据(例如,从/dev/urandomopenssl_random_pseudo_bytes).

Yes, you can obtain some random data (e.g. from /dev/urandom, or openssl_random_pseudo_bytes).

然后使用例如bcmath或gmp.有关如何操作的信息,请参见此答案.

Then convert the data into its decimal representation, using e.g. bcmath or gmp. See this answer on how to do that.

这篇关于如何在PHP中生成超过32位限制的随机数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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