首页 > 解决方案 > 如何指定具有最大位数的 BigInteger 类型的 BigInteger?

问题描述

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

BigInteger(int numBits, Random rnd)

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

标签: javabiginteger

解决方案


你不能。没有构造函数需要BigInteger多个位。

为什么不?BigInteger不会在内部将位数存储为大整数。它包含int许多位,一个反映在公共 API 中的设计决策:

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

推荐阅读