java生成非常大的随机数

发布于 2024-12-17 19:02:25 字数 98 浏览 4 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

帅气称霸 2024-12-24 19:02:25

好吧,一种方法是访问 Random.org 并下载一个二进制随机文件。这些文件是由大气噪声生成的,因此非常随机。我用它作为我的国际象棋引擎中的 Zobrist 键。

或者你也可以去那里,

BigInteger b = new BigInteger(256, new Random());

那里会给你你想要的东西。在此示例中,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

BigInteger b = new BigInteger(256, new Random());

which will give you what you want. In this example, a BigInteger consisting of 256 bits.

清晰传感 2024-12-24 19:02:25

组合随机。 nextBytes(byte[])BigInteger(byte[] )

import java.util.*;
import java.math.*;
class Test{
    public static void main(String[]_){

        int n = 16;

        Random r = new Random();
        byte[] b = new byte[n];
        r.nextBytes(b);
        BigInteger i = new BigInteger(b);

        System.out.println(i);
    }
}

Combine Random.nextBytes(byte[]) with BigInteger(byte[]).

import java.util.*;
import java.math.*;
class Test{
    public static void main(String[]_){

        int n = 16;

        Random r = new Random();
        byte[] b = new byte[n];
        r.nextBytes(b);
        BigInteger i = new BigInteger(b);

        System.out.println(i);
    }
}
迷途知返 2024-12-24 19:02:25

您只需输入:

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文