随机生成1-6的数字

掷骰子游戏:规定游戏者掷出的骰子==7,为赢

1,用Math.ceil(Math.random()*6);时,主要获取1到6的随机整数,取0的几率极小。
2,用Math.round(Math.random()*5 + 1),可基本均衡获取1到6的随机整数,其中获取最小值0和最大值6的几率少一半。
3,用Math.floor(Math.random()*6 + 1);时,可均衡获取1到6的随机整数。
public class TouZiClass {
    public static void main(String[] args) {
        int r1 = (int)(Math.floor(Math.random()*6+1));
        int r2 = (int)(Math.floor(Math.random()*6+1));
        boolean isTrue = isTrue(r1,r2);
        System.out.println(isTrue);
    }
    public static boolean isTrue(int r1,int r2){
        if (r1+r2 == 7){
            return true;
        }
        return false;
    }
}

猜你喜欢

转载自blog.csdn.net/xy58451921/article/details/130101967
1-6