java - Dice Sum Probability with Different types of Dice -


i working on java application need calculate probabilities of rolling each sum variety of dice. dice types supporting d4 (4 sided dice), d6 (6 sided dice), d8 (8 sided dice), d10, d12, , d20. user able input number of each type of dice want use in calculation. example, user may enter 6 d6 , 4 d4.

with given information (the number of dice of each type), looking calculate probability each possible sum rolled. using information create chart showing probability distribution selected combination of dice provided.

this application being written in java.

where @ have function calculate probability of specific sum using 1 size of dice

/*     recursively calculates probability of rolling particular number     when rolling multiple dice of 1 type     @param dice number of dice     @param seekedvalue value probability being calculated     @param sides number of sides on type of die      */     private double diceprob(int dice, int seekedvalue, int sides){         if (dice == 0){             if (seekedvalue == 0){                 return 1.0;             } else {                 return 0.0;             }         } else {             double sum = 0;             (int = seekedvalue - sides; < seekedvalue; i++){                 sum += diceprob(dice -1, i, sides) / sides;             }             return sum;         }      } 

i use code find possible probabilites

/* variable explanations: diceentries: array list contains number of each dice supplied user. ordered number of sides, d4 @ beginning , d20 @ end dicevalues: array contains sides of dice types probarray: array list contain probabilities of each sum possible min: minimum sum  possible max: maximum sum possible */ arraylist<integer> diceentries arraylist<float> probarray = new arraylist<>(); int[] dicevalues = {4,6,8,10,12,20}; float prob = 0; (int = min; <= max; i++){     (int j = 0; j <= 5; j++) {         prob = (float) diceprob(diceentries.get(j), i, dicevalues[j]);         if (prob != 0) {             probarray.add(prob);         }     } } 

my current code able handle dice of 1 size, ie d6s or d4s , not mix of them.

if community can provide guidance, appreciated. open on approaches well. example, have read generating functions might better way of doing this, combinatorics statistics bit weak , if did have way of coding up, nice see it.

thanks lot folks

another entry brute force method, using list of integers(dice sides) handle multiple die types. advantage if want lot of probabilities, can run once query various probabilities. disadvantage brute force method it's inefficient getting single probability.

public int[] probs;  public void genrolls(int sum, list<integer> sides) {     if (sides.size() == 0)     {         probs[sum]++;         return;     }     int top = sides.get(0);     (int x = 1; x <= top; x++)         genrolls(sum+x, sides.sublist(1, sides.size())); }  public void diceprob(int target, list<integer> sides) {     int maxval = 0;     double possibilities = 1;     (integer : sides)     {         maxval+= i;         possibilities *= i;     }     probs = new int[maxval+1];     genrolls(0, sides);     system.out.println("probability " + (probs[target]/possibilities)); } 

Comments

Popular posts from this blog

shopping cart - Page redirect not working PHP -

php - How to modify a menu to show sub-menus -

python - Installing PyDev in eclipse is failed -