簡體   English   中英

java生成30到32位數之間的隨機數

[英]java generate random numbers between range of 30 and 32 digits

閱讀其他帖子我認為我將不得不使用BigInteger在30到32位數字之間生成20,000個隨機數。

public BigInteger(int numBits, Random rnd)

但這不允許數字的最小和最大范圍。

謝謝

如果你想使用這個功能,你可以這樣做

public static BigInteger random(Random rand, BigInteger minValue, BigInteger maxValue) {
    BigInteger range = maxValue.subtract(minValue).add(BigInteger.ONE);
    int bits = range.bitLength();
    BigInteger ret;
    do {
        ret = new BigInteger(bits, rand);
    } while(ret.compareTo(range) >= 0);
    return ret.add(minValue);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM