如何指定具有最大位数的 BigInteger 类型的 BigInteger?

How can you specify a BigInteger with a maximum number of bits of type BigInteger?

我知道 BigInteger 有一个构造函数,您可以在其中通过传递新 BigInteger 的最大 bitLength 和随机参数来生成随机 BigInteger:

BigInteger(int numBits, Random rnd)

如何生成一个随机的 BitInteger,其中 numBits 的类型是 BitInteger 而不是 int? 注意:我不想做 myBitInteger.intValue().

你不能。没有采用 BigInteger 位数的构造函数。

为什么不呢? BigInteger 不会在内部将位数存储为大整数。它拥有 int 个位数,一个设计决策反映在 public API:

BigInteger(int numBits, Random rnd);
int bitCount();
int bitLength();
static BigInteger probablePrime(int bitLength, Random rnd);