模拟T次投掷骰子

一 代码

package Rolls;

import Counter.Counter;
import common.StdOut;
import common.StdRandom;

/**
* Copyright (C), 2020-2020, XXX有限公司
* FileName: Rolls
* Author:   cakin
* Date:     2020/1/11
* Description: 模拟投掷骰子
*/
public class Rolls {
    public static void main( String[] args ) {
        int T = Integer.parseInt(args[0]);
        int SIDES = 6;
        Counter[] rolls = new Counter[SIDES+1];
        for(int i=1;i<=SIDES;i++){
            rolls[i] = new Counter(i+"'s");
        }
        for(int t=0;t<T;t++){
            int result = StdRandom.uniform(1,SIDES+1);
            rolls[result].increment();
        }
        for (int i=1;i<=SIDES;i++){
            StdOut.println(rolls[i]);
        }
    }
}

二 测试结果

当投掷1000000次的结果

166995 1's
166249 2's
166429 3's
167018 4's
166639 5's
166670 6's

三 代码参考

https://gitee.com/cakin24/Algorithm

发布了4080 篇原创文章 · 获赞 538 · 访问量 296万+

猜你喜欢

转载自blog.csdn.net/chengqiuming/article/details/103938759