在java中设置随机BigInteger的长度

时间:2022-05-05 17:14:21

Suppose I take an input "8" from the user, I should be able to generate a random BigInteger of length 8 digits. Suppose I take an input of "20", I should be able to generate a random BigInteger of length 20 digits. How can I achieve this?

假设我从用户那里取一个输入“8”,我应该能够生成一个长度为8位的随机BigInteger。假设我输入“20”,我应该能够生成一个长度为20位的随机BigInteger。我怎样才能做到这一点?

I have the following code that I have referred from an example.

我有一个例子中提到的以下代码。

int SIZE = 512;
p = new BigInteger(SIZE, 15, new Random());
q = new BigInteger(SIZE, 15, new Random());

Can anyone tell me what those arguments mean? Or else can you suggest an easier method to achieve this?

谁能告诉我这些论点是什么意思?或者你能建议一种更简单的方法来实现这一目标吗?

2 个解决方案

#1


0  

BigInteger(int bitLength, int certainty, Random rnd)

BigInteger(int bitLength,int certainty,Random rnd)

Constructs a randomly generated positive BigInteger that is probably prime, with the specified bitLength. It is recommended that the probablePrime method be used in preference to this constructor unless there is a compelling need to specify a certainty.

使用指定的bitLength构造一个随机生成的可能为素数的正BigInteger。建议使用probablePrime方法优先于此构造函数,除非迫切需要指定确定性。

Parameters:

bitLength - bitLength of the returned BigInteger.

bitLength - 返回的BigInteger的bitLength。

certainty - a measure of the uncertainty that the caller is willing to tolerate. The probability that the new BigInteger represents a prime number will exceed (1 - 1/2certainty). The execution time of this constructor is proportional to the value of this parameter.

确定性 - 衡量呼叫者愿意容忍的不确定性的指标。新BigInteger表示素数的概率将超过(1 - 1/2)。此构造函数的执行时间与此参数的值成比例。

rnd - source of random bits used to select candidates to be tested for primality.

rnd - 用于选择要测试素数的候选者的随机比特源。

taken straight from Oracles website, hopefully this is what you were looking for.

直接从Oracles网站上获取,希望这正是您所寻找的。

#2


-1  

An integer solution

整数解

public static int randInt(int min, int max) {

    // Usually this can be a field rather than a method variable
    Random rand = new Random();

    // nextInt is normally exclusive of the top value,
    // so add 1 to make it inclusive
    int randomNum = rand.nextInt((max - min) + 1) + min;

    return randomNum;
}

so if you need 8 digit random number

所以如果你需要8位数的随机数

call this function with range in 8 digits i.e smallest 8 digit # and highest 8 digit number .

称此功能的范围为8位,即最小的8位#和最高的8位数。

e.g

randInt(10000000, 99999999)

Source: the code for random number for a range is taken from here

来源:范围的随机数代码取自此处

How do I generate random integers within a specific range in Java?

如何在Java中生成特定范围内的随机整数?

you can look at nextLong() too. These are uniformly generated random numbers

你也可以看看nextLong()。这些是统一生成的随机数

http://docs.oracle.com/javase/6/docs/api/java/util/Random.html#nextLong()

#1


0  

BigInteger(int bitLength, int certainty, Random rnd)

BigInteger(int bitLength,int certainty,Random rnd)

Constructs a randomly generated positive BigInteger that is probably prime, with the specified bitLength. It is recommended that the probablePrime method be used in preference to this constructor unless there is a compelling need to specify a certainty.

使用指定的bitLength构造一个随机生成的可能为素数的正BigInteger。建议使用probablePrime方法优先于此构造函数,除非迫切需要指定确定性。

Parameters:

bitLength - bitLength of the returned BigInteger.

bitLength - 返回的BigInteger的bitLength。

certainty - a measure of the uncertainty that the caller is willing to tolerate. The probability that the new BigInteger represents a prime number will exceed (1 - 1/2certainty). The execution time of this constructor is proportional to the value of this parameter.

确定性 - 衡量呼叫者愿意容忍的不确定性的指标。新BigInteger表示素数的概率将超过(1 - 1/2)。此构造函数的执行时间与此参数的值成比例。

rnd - source of random bits used to select candidates to be tested for primality.

rnd - 用于选择要测试素数的候选者的随机比特源。

taken straight from Oracles website, hopefully this is what you were looking for.

直接从Oracles网站上获取,希望这正是您所寻找的。

#2


-1  

An integer solution

整数解

public static int randInt(int min, int max) {

    // Usually this can be a field rather than a method variable
    Random rand = new Random();

    // nextInt is normally exclusive of the top value,
    // so add 1 to make it inclusive
    int randomNum = rand.nextInt((max - min) + 1) + min;

    return randomNum;
}

so if you need 8 digit random number

所以如果你需要8位数的随机数

call this function with range in 8 digits i.e smallest 8 digit # and highest 8 digit number .

称此功能的范围为8位,即最小的8位#和最高的8位数。

e.g

randInt(10000000, 99999999)

Source: the code for random number for a range is taken from here

来源:范围的随机数代码取自此处

How do I generate random integers within a specific range in Java?

如何在Java中生成特定范围内的随机整数?

you can look at nextLong() too. These are uniformly generated random numbers

你也可以看看nextLong()。这些是统一生成的随机数

http://docs.oracle.com/javase/6/docs/api/java/util/Random.html#nextLong()