Class CumulativeDistribution<T>


  • public class CumulativeDistribution<T>
    extends java.lang.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.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      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 clear()
      Removes all the values from the distribution
      void generate()
      Generate the cumulative distribution
      void generateNormalized()
      Generate the cumulative distribution in [0,1] where each interval will get a frequency between [0,1]
      void generateUniform()
      Generate the cumulative distribution in [0,1] where each value will have the same frequency and interval size
      float getInterval​(int index)  
      T 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 size()  
      T value()  
      T 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 Detail

      • CumulativeDistribution

        public CumulativeDistribution()
    • Method Detail

      • 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.
        Parameters:
        probability -
        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