Android Open Source - dice-probabilities Die Distribution






From Project

Back to project page dice-probabilities.

License

The source code is released under:

MIT License

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;
/*ww  w  .  j  a  v  a 2s .  c  o m*/
import org.apache.commons.math3.fraction.BigFraction;

/**
 * <p>
 * This represents the distribution for a single die of some number of sides.
 * Each probability in the range 1 to sides is of equal value and sums to 1.
 */
public class DieDistribution extends AbstractDistribution {

  // the total number of sides of the die
  private int sides;

  // the equal probability of getting any side of the die
  // cache this for efficiency
  private BigFraction probability;

  public DieDistribution(int sides) {
    assert (sides > 0);
    this.sides = sides;
    this.probability = new BigFraction(1, sides);
  }

  public int getSides() {
    return sides;
  }

  @Override
  public int lowerBound() {
    return 1;
  }

  @Override
  public int upperBound() {
    // sides are one based. e.g. a six sided die uses the x values
    // 1 through 6 inclusive
    return sides + 1;
  }

  @Override
  public BigFraction getProbabilityBounded(int x) {
    return probability;
  }

  /**
   * <p>
   * Slightly more efficient than the default getCumulativeProbability
   */
  @Override
  public BigFraction getCumulativeProbability(int x) {
    if (x >= 1 && x <= sides) {
      return probability.multiply(sides - x + 1);
    } else if (x > sides) {
      return BigFraction.ZERO;
    } else {
      return BigFraction.ONE;
    }
  }
}




Java Source Code List

com.asolutions.widget.RowLayout.java
org.kleemann.diceprobabilities.Check.java
org.kleemann.diceprobabilities.ConstantCurrentDicePile.java
org.kleemann.diceprobabilities.CurrentDicePile.java
org.kleemann.diceprobabilities.DiceSet.java
org.kleemann.diceprobabilities.MainActivity.java
org.kleemann.diceprobabilities.PoolDicePile.java
org.kleemann.diceprobabilities.TargetPool.java
org.kleemann.diceprobabilities.Target.java
org.kleemann.diceprobabilities.distribution.AbstractDistribution.java
org.kleemann.diceprobabilities.distribution.CachedCumulativeDistribution.java
org.kleemann.diceprobabilities.distribution.ConstantDistribution.java
org.kleemann.diceprobabilities.distribution.CritDistribution.java
org.kleemann.diceprobabilities.distribution.DeathZoneDieDistribution.java
org.kleemann.diceprobabilities.distribution.DieDistribution.java
org.kleemann.diceprobabilities.distribution.Distribution.java
org.kleemann.diceprobabilities.distribution.DogslicerDistribution.java
org.kleemann.diceprobabilities.distribution.ScaleCumulativeDistribution.java
org.kleemann.diceprobabilities.distribution.SumDistribution.java
org.kleemann.diceprobabilities.graph.GraphView.java
org.kleemann.diceprobabilities.graph.Interpolate.java
org.kleemann.diceprobabilities.graph.Point.java
org.kleemann.diceprobabilities.graph.Vector.java
org.kleemann.diceprobabilities.special.AbstractSpecial.java
org.kleemann.diceprobabilities.special.CritSpecial.java
org.kleemann.diceprobabilities.special.DeathZoneSpecial.java
org.kleemann.diceprobabilities.special.DogslicerSpecial.java
org.kleemann.diceprobabilities.special.FailureSpecial.java
org.kleemann.diceprobabilities.special.ForcedRerollSpecial.java
org.kleemann.diceprobabilities.special.ModifyEachDieSpecial.java
org.kleemann.diceprobabilities.special.NormalSpecial.java
org.kleemann.diceprobabilities.special.SecondChanceSpecial.java
org.kleemann.diceprobabilities.special.SpecialSpinner.java
org.kleemann.diceprobabilities.special.Special.java