繁体   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