(당신이 :-). 자신을 nMin
및 nMax
을 조정해야하는 방법 당신은 아마의 수학을 할 수 있습니다) 그, BigInteger
.
this question의 정보를 사용하여 임의의 BigInteger
을 만들고, 심지어 짝수 인 경우 1을 추가하면됩니다.
BigInteger randomOdd(BigInteger min, BigInteger max) {
BigInteger range = max.subtract(min);
// expected iterations: 2 - max iterations: infinite
BigInteger tmp;
do {
tmp = new BigInteger(n.bitLength(), rng); // rng is your Random Number Generator
} while (tmp.compareTo(range) >= 0);
BigInteger result = min.add(tmp);
// force the result to be odd
// TODO: will this push it over max?
result = result.or(BigInteger.ONE);
return result;
}
또는, BigInteger를 클래스의 메소드를 사용할 수 있습니다 BigInteger.probablePrime()
을 :
public static BigInteger probablePrime(int bitLength, Random rnd)
지정된 bitLength
으로, 아마 소수 긍정적 인 BigInteger
를 돌려줍니다. 이 메소드가 돌려주는 BigInteger가 합성 인 확률은 2^100
를 넘지 않습니다.
매개 변수 :
- 비트 수 - 반환
BigInteger
의 비트 수.
- rnd - 소수성을 테스트 할 후보를 선택하는 데 사용되는 임의 비트의 소스입니다.
결과 :
소수 비트 수 비트의 BigInteger
, 또한 아마 이상한.
"int 범위 밖"- 'long'외에도? – Mysticial
예, 이중이어야합니다 –
이 경우 아마도 디자인에 문제가있을 것입니다.'long'보다 긴 정수의 균일 한 분포를 생성하는 경우 생성 된 수는 홀수와 짝수를 구별하기 위해 'double'에서 충분히 작고 (따라서 충분히 정확할 확률이 높을 것입니다. 사라지는 작은. – Mysticial