java生成非常大的随机数
java中如何生成非常大的随机数?我说的是 10000 位数字?我知道我们必须使用 BigInteger,但是我们该怎么做呢?做这样的事情最有效的方法是什么?请提供一个小例子。谢谢。
How can we generate very large random number in java? I am talking something like 10000 digits? I know we have to use BigInteger but how can we do this? What is the most efficent way of doing something like this? Please provide a small example. Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好吧,一种方法是访问 Random.org 并下载一个二进制随机文件。这些文件是由大气噪声生成的,因此非常随机。我用它作为我的国际象棋引擎中的 Zobrist 键。
或者你也可以去那里,
那里会给你你想要的东西。在此示例中,BigInteger 由 256 位组成。
Well, one way is to go to Random.org and download a one of the binary random files. The files are generated from atmospheric noise, so it's very random. I used it for Zobrist keys in my chess engine.
Alternatively you could go
which will give you what you want. In this example, a BigInteger consisting of 256 bits.
组合
随机。 nextBytes(byte[])
与BigInteger(byte[] )
。Combine
Random.nextBytes(byte[])
withBigInteger(byte[])
.您只需输入:
int number = (int)(Math.random() * 100);
此外,如果更改乘数,您还可以生成更大的数字:
int number = (int )(Math.random() * 1000);
PS 您不需要导入类。
You can just type:
int number = (int)(Math.random() * 100);
Also, you can generate even bigger numbers if you change the multiplier:
int number = (int)(Math.random() * 1000);
P.S. You don't need to import a class.