如何使用 BigInteger 类在 Java 中生成一个随机的 n 位整数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3709521/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 03:06:08  来源:igfitidea点击:

How do I generate a random n digit integer in Java using the BigInteger class?

javabigintegerdigits

提问by makaveli2178

I am unsure about how to generate a random n digit integer in Java using the BigInteger class.

我不确定如何使用 BigInteger 类在 Java 中生成一个随机的 n 位整数。

采纳答案by Eyal Schneider

private static Random rnd = new Random();

public static String getRandomNumber(int digCount) {
    StringBuilder sb = new StringBuilder(digCount);
    for(int i=0; i < digCount; i++)
        sb.append((char)('0' + rnd.nextInt(10)));
    return sb.toString();
}

And then you can use it:

然后你可以使用它:

new BigInteger(getRandomNumber(10000))

回答by Carl

According to the docs, there is a constructor to do what you want in java 6: BigInteger(int, java.util.Random)

根据文档,有一个构造函数可以在 java 6 中执行您想要的操作:BigInteger(int, java.util.Random)

To that, you need only add a randomly selected 5000th digit-i.e. Use the rng constructor to 4999 digits, the add the last in via a separate random process. Actually, since you want to just sample performance for large values, you could generate the bits, and tack a one bit on the big end, rather than slave to decimal notation.

为此,您只需要添加一个随机选择的第 5000 位数字,即使用 rng 构造函数添加到 4999 位数字,通过单独的随机过程添加最后一位。实际上,由于您只想对大值的性能进行采样,您可以生成位,并在大端添加一位,而不是从属十进制表示法。

回答by Jon Skeet

The simplest way would probably to be to fill a char[] array with 5000 random digits, convert that to a string, and then call the BigInteger(String)constructor.

最简单的方法可能是用 5000 个随机数字填充 char[] 数组,将其转换为字符串,然后调用BigInteger(String)构造函数。

If any of those steps gives you problems, please give more details.

如果这些步骤中的任何一个给您带来问题,请提供更多详细信息。

Alternatively, you coulddo something like this:

或者,您可以执行以下操作:

Random rng = new Random(); // But use one instance throughout your app
BigInteger current = BigInteger.ZERO;
for (int i = 0; i < 5000; i++) {
    BigInteger nextDigit = BigInteger.valueOf(rng.nextInt(10));
    current = current.multiply(BigInteger.TEN).add(nextDigit);
}

I suspect that would be rather less efficient though.

不过,我怀疑这会效率较低。

You could reduce the number of steps required by generating nine random digits at a time, with rng.nextInt(1000000000).

您可以通过一次生成九个随机数字来减少所需的步骤数,使用rng.nextInt(1000000000).

回答by Sean Patrick Floyd

Here are two versions, one takes a Random as parameter (in case you want to re-use it):

这里有两个版本,一个以 Random 作为参数(以防你想重用它):

public static BigInteger getRandomNumber(final int digCount){
    return getRandomNumber(digCount, new Random());
}

public static BigInteger getRandomNumber(final int digCount, Random rnd){
    final char[] ch = new char[digCount];
    for(int i = 0; i < digCount; i++){
        ch[i] =
            (char) ('0' + (i == 0 ? rnd.nextInt(9) + 1 : rnd.nextInt(10)));
    }
    return new BigInteger(new String(ch));
}

The resulting BigInteger will always have the specified length.

生成的 BigInteger 将始终具有指定的长度。

回答by venu88

If n is between 1 to 12 then following method helps

如果 n 介于 1 到 12 之间,则以下方法会有所帮助

private String getRandom(int length) {
    if (length < 1 && length > 12) {
        throw new IllegalArgumentException("Random number generator length should be between 1 to 12");
    }
    long nextLong = Math.abs(random.nextLong());
    return String.valueOf(nextLong).substring(0, length);
}

One more thing to note is that it is not well tested code.

还要注意的一件事是,它不是经过良好测试的代码。

回答by Dheeraj Joshi

Take a string with 5000 digits in it then convert it into BigInteger.

取一个包含 5000 位数字的字符串,然后将其转换为 BigInteger。