If you think the Android project dice-probabilities listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
Java Source Code
package org.kleemann.diceprobabilities.distribution;
/*www.java2s.com*/import org.apache.commons.math3.fraction.BigFraction;
/**
* <p>
* A distribution that is similar to DieDistribution except that the highest
* value of the die receives a bonus. Note: this makes CritDistribution not a
* DieDistribution since DieDistributions are equal probabilities between 1 and
* n.
*
* <p>
* e.g. a d6 is {1,2,3,4,5,6} but a crit +1 d6 is {1,2,3,4,5,7}
*/publicclass CritDistribution extends AbstractDistribution {
// the total number of sides of the die
privatefinalint sides;
privatefinalint bonus;
// the equal probability of getting any side of the die
// cache this for efficiency
privatefinal BigFraction probability;
public CritDistribution(int sides, int bonus) {
assert (sides > 0);
this.sides = sides;
this.bonus = bonus;
this.probability = new BigFraction(1, sides);
}
@Override
publicint lowerBound() {
return 1;
}
@Override
publicint upperBound() {
return sides + 1 + bonus;
}
@Override
public BigFraction getProbability(int x) {
if ((x >= 1 && x < sides) || x == sides + bonus) {
return probability;
} else {
return BigFraction.ZERO;
}
}
}