如何在1和任何东西之间生成随机BigInteger数字?

时间:2021-11-20 16:49:33

Im trying to generate random numbers and i using BigInteger class to check if the generated numer is prime with probablePrime();

我试图生成随机数,我使用BigInteger类来检查生成的数字是否是使用probablePrime()的素数;

Here is my code:

这是我的代码:

btnRun.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Random prng = new Random();

            Toast.makeText(getBaseContext()," " + BigInteger.probablePrime(10,prng), Toast.LENGTH_SHORT).show();

        }
    });

I dont really understand the first parameter in the probablePrime() wich in my case is 10. I Know its the byte off the int value, is this effecting the range of the generated numbers? Im only get values from 523-1003 BUT i want to generate random numbers from 1-anything and check them in the probablePrime(); method.

我真的不明白probablePrime()中的第一个参数,在我的情况下是10.我知道它的int值的字节,这是否会影响生成数字的范围?我只得到523-1003的值,但我想从1-anything中生成随机数并在probablePrime()中检查它们;方法。

How is that possible?

怎么可能?

1 个解决方案

#1


Look at the javadoc for BigInteger.randomPrime(int, Random):

查看BigInteger.randomPrime的javadoc(int,Random):

public static BigInteger probablePrime(int bitLength, Random rnd)

public static BigInteger probablePrime(int bitLength,Random rnd)

Returns a positive BigInteger that is probably prime, with the specified bitLength. The probability that a BigInteger returned by this method is composite does not exceed 2-100.

使用指定的bitLength返回可能为素数的正BigInteger。此方法返回的BigInteger复合的概率不超过2-100。

Parameters:

  • bitLength - bitLength of the returned BigInteger. [emphasis mine]
  • bitLength - 返回的BigInteger的bitLength。 [强调我的]

  • rnd - source of random bits used to select candidates to be tested for primality.
  • rnd - 用于选择要测试素数的候选者的随机比特源。

You will notice that the parameter is the number of bits in the prime to get, so 10 means 0 through 1023 are candidates.

您会注意到参数是要获取的素数中的位数,因此10表示0到1023是候选。

If you want primes up to a specific limit, you should determine how many bits you want:

如果您希望质数达到特定限制,则应确定所需的位数:

int numBits = Integer.highestOneBit(yourLimit);

and discard primes that are greater than your limit.

并丢弃超过限额的素数。

#1


Look at the javadoc for BigInteger.randomPrime(int, Random):

查看BigInteger.randomPrime的javadoc(int,Random):

public static BigInteger probablePrime(int bitLength, Random rnd)

public static BigInteger probablePrime(int bitLength,Random rnd)

Returns a positive BigInteger that is probably prime, with the specified bitLength. The probability that a BigInteger returned by this method is composite does not exceed 2-100.

使用指定的bitLength返回可能为素数的正BigInteger。此方法返回的BigInteger复合的概率不超过2-100。

Parameters:

  • bitLength - bitLength of the returned BigInteger. [emphasis mine]
  • bitLength - 返回的BigInteger的bitLength。 [强调我的]

  • rnd - source of random bits used to select candidates to be tested for primality.
  • rnd - 用于选择要测试素数的候选者的随机比特源。

You will notice that the parameter is the number of bits in the prime to get, so 10 means 0 through 1023 are candidates.

您会注意到参数是要获取的素数中的位数,因此10表示0到1023是候选。

If you want primes up to a specific limit, you should determine how many bits you want:

如果您希望质数达到特定限制,则应确定所需的位数:

int numBits = Integer.highestOneBit(yourLimit);

and discard primes that are greater than your limit.

并丢弃超过限额的素数。