我有一个循环来生成两个素数,我不希望它们相等,它们都需要完全是“数字”数字。我可以让第一个素数(bigInt1)具有所需的长度,但第二个(bigInt2)从“digits”到“digits + 1”不等,我不知道为什么,我花了很多时间查看这段代码我只是找不到解决方案,有人可以帮忙吗?
...
public static BigInteger[] bigInts = new BigInteger[2];
static int digits;
public static void GeneratePrimeBigInt(String stringDigits){
digits = Integer.parseInt(stringDigits);
int bits = (int)Math.ceil(Math.log(Math.pow(10,digits))/(Math.log(2))); // convert digits to bits
// Generate Huge prime Random Number with 1 - 2^(-1000) probability of being prime
BigInteger bigInt1 = new BigInteger(bits,1000,new Random(System.currentTimeMillis()));
BigInteger bigInt2 = new BigInteger(bits,1000,new Random(System.currentTimeMillis()));
while (bigInt1.toString().length() != digits){
bigInt1 = new BigInteger(bits,1000,new Random(System.currentTimeMillis()));
}
// make sure no two bigIntegers are the same
while(bigInt1.equals(bigInt2)){
BigInteger bigInt21 = new BigInteger(bits,1000,new Random(System.currentTimeMillis()));
bigInt2 = bigInt21;
if ((bigInt2.toString().length()) != digits){
while (bigInt2.toString().length() != digits){
BigInteger bigInt22 = new BigInteger(bits,1000,new Random(System.currentTimeMillis()));
bigInt2 = bigInt22;
}
}
}
// store results in array for future reference and display results in RsaWindow
RsaWindow.setMyLabels(5, "Here are two prime numbers, p and q,
of " + digits + "digits");
bigInts[0] = bigInt1;
RsaWindow.setMyLabels(7,"p= " + bigInt1.toString());
bigInts[1] = bigInt2;
RsaWindow.setMyLabels(8,"q= " + bigInt2.toString());
}