Package arc.math

Class CumulativeDistribution<T>

java.lang.Object
arc.math.CumulativeDistribution<T>

public class CumulativeDistribution<T> extends Object
This class represents a cumulative distribution. It can be used in scenarios where there are values with different probabilities and it's required to pick one of those respecting the probability. For example one could represent the frequency of the alphabet letters using a cumulative distribution and use it to randomly pick a letter respecting their probabilities (useful when generating random words). Another example could be point generation on a mesh surface: one could generate a cumulative distribution using triangles areas as interval size, in this way triangles with a large area will be picked more often than triangles with a smaller one. See Wikipedia for a detailed explanation.
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    class 
     
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    add(T value)
    Adds a value with interval size equal to zero to the distribution
    void
    add(T value, float intervalSize)
    Adds a value with a given interval size to the distribution
    void
    Removes all the values from the distribution
    void
    Generate the cumulative distribution
    void
    Generate the cumulative distribution in [0,1] where each interval will get a frequency between [0,1]
    void
    Generate the cumulative distribution in [0,1] where each value will have the same frequency and interval size
    float
    getInterval(int index)
     
    getValue(int index)
     
    void
    setInterval(int index, float intervalSize)
    Sets the interval size for the value at the given index
    void
    setInterval(T obj, float intervalSize)
    Set the interval size on the passed in object.
    int
     
     
    value(float probability)
    Finds the value whose interval contains the given probability Binary search algorithm is used to find the value.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • CumulativeDistribution

      public CumulativeDistribution()
  • Method Details

    • add

      public void add(T value, float intervalSize)
      Adds a value with a given interval size to the distribution
    • add

      public void add(T value)
      Adds a value with interval size equal to zero to the distribution
    • generate

      public void generate()
      Generate the cumulative distribution
    • generateNormalized

      public void generateNormalized()
      Generate the cumulative distribution in [0,1] where each interval will get a frequency between [0,1]
    • generateUniform

      public void generateUniform()
      Generate the cumulative distribution in [0,1] where each value will have the same frequency and interval size
    • value

      public T value(float probability)
      Finds the value whose interval contains the given probability Binary search algorithm is used to find the value.
      Returns:
      the value whose interval contains the probability
    • value

      public T value()
      Returns:
      the value whose interval contains a random probability in [0,1]
    • size

      public int size()
      Returns:
      the amount of values
    • getInterval

      public float getInterval(int index)
      Returns:
      the interval size for the value at the given position
    • getValue

      public T getValue(int index)
      Returns:
      the value at the given position
    • setInterval

      public void setInterval(T obj, float intervalSize)
      Set the interval size on the passed in object. The object must be present in the distribution.
    • setInterval

      public void setInterval(int index, float intervalSize)
      Sets the interval size for the value at the given index
    • clear

      public void clear()
      Removes all the values from the distribution