喝酒摇骰子概率
昨晚喝酒摇骰子,喝了不少酒。为了少喝点写个简单代码计算下摇色子概率。
规则:
1、一点可以当成1点或者其他任意点数。
2、每人蛊中有5个骰子。
3、从低往高叫全场最少有多少个数字num点数。
double p = 0; while (num <= dice){ p += Math.pow(1 / 6.0, num) * Math.pow(5 / 6.0, dice - num) * arrange(num,dice); num++; }
点数大于等于num出现的概率相加。
public class Test { public static void main(String[] args) { double p2 = caculateProbability1(6, 8 * 5); double p1 = caculateProbability1(7, 8 * 5); //人头一 double p = caculateProbability1(8, 8 * 5); double p3 = caculateProbability1(9, 8 * 5); double p32 = caculateProbability2_6(10, 8 * 5); double p33 = caculateProbability2_6(11, 8 * 5); double p34 = caculateProbability2_6(12, 8 * 5); double p35 = caculateProbability2_6(13, 8 * 5); double p4 = caculateProbability2_6(14, 8 * 5); double p5 = caculateProbability2_6(15, 8 * 5); double p6 = caculateProbability2_6(16, 8 * 5); double p7 = caculateProbability2_6(17, 8 * 5); //返回叫10-20的概率 double[] pa0 = stragety1(8 * 5,0); double[] pa1 = stragety1(8 * 5,1); double[] pa2 = stragety1(8 * 5,2); double[] pa3 = stragety1(8 * 5,3); double[] pa4 = stragety1(8 * 5,4); double[] pa5 = stragety1(8 * 5,5); //返回叫5-10的概率 double[] pa01 = stragety2_6(8 * 5,0); double[] pa11 = stragety2_6(8 * 5,1); double[] pa21 = stragety2_6(8 * 5,2); double[] pa31 = stragety2_6(8 * 5,3); double[] pa41 = stragety2_6(8 * 5,4); double[] pa51 = stragety2_6(8 * 5,5); System.out.println(p); } /** * 策略概率集合,已有1数字个数 * */ private static double[] stragety1(double dice, double myNum) { double[] doubles = new double[6]; for (int i = 5; i <= 10; i++) { doubles[i-5] = Math.round(caculateProbability1(i-myNum,dice-5) * 100)/100.0; } return doubles; } /** * 策略概率集合,已有2-6数字个数 * */ private static double[] stragety2_6(double dice, double myNum) { double[] doubles = new double[11]; for (int i = 10; i <= 20; i++) { doubles[i-10] = Math.round(caculateProbability2_6(i-myNum,dice-5) * 100)/100.0; } return doubles; } /** * 计算骰子出现1点的概率 * */ private static double caculateProbability1(double num, double dice) { double p = 0; while (num <= dice){ p += Math.pow(1 / 6.0, num) * Math.pow(5 / 6.0, dice - num) * arrange(num,dice); num++; } return p; } /** * 计算骰子出现2-6点的概率 * */ private static double caculateProbability2_6(double num, double dice) { double p = 0; while (num <= dice){ p += Math.pow(2 / 6.0, num) * Math.pow(4 / 6.0, dice - num) * arrange(num,dice); num++; } return p; } /** * 计算排列组合情况 * n 总数 * m 需要排列的元素个数 * */ private static double arrange(double m, double n){ if (m > n/2){ m = n - m; } double arrange = 1; while (m > 0){ arrange *= n / m; m--; n--; } return arrange; } }
下面是八个人玩跑的结果:
在知道自己摇到骰子个数的情况下,叫多少个的概率