Math 中 random 的用法

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wpwbb510582246/article/details/83547127
//    产生一个从 a 到 b 的随机数的方法 : 先用 random.nextDouble() 产生一个从 0 到 1 的小数,然后乘以 (b - a) , 然后再加上 a , 然后再将得到的结果转化为整数.
private static int getRandom(int a, int b) {
    return (int)(Math.random() * (b - a) + a);
}

猜你喜欢

转载自blog.csdn.net/wpwbb510582246/article/details/83547127