Package com.badlogic.gdx.math
Class CumulativeDistribution<T>
- java.lang.Object
-
- com.badlogic.gdx.math.CumulativeDistribution<T>
-
public class CumulativeDistribution<T> extends java.lang.ObjectThis 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 classCumulativeDistribution.CumulativeValue
-
Constructor Summary
Constructors Constructor Description CumulativeDistribution()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidadd(T value)Adds a value with interval size equal to zero to the distributionvoidadd(T value, float intervalSize)Adds a value with a given interval size to the distributionvoidclear()Removes all the values from the distributionvoidgenerate()Generate the cumulative distributionvoidgenerateNormalized()Generate the cumulative distribution in [0,1] where each interval will get a frequency between [0,1]voidgenerateUniform()Generate the cumulative distribution in [0,1] where each value will have the same frequency and interval sizefloatgetInterval(int index)TgetValue(int index)voidsetInterval(int index, float intervalSize)Sets the interval size for the value at the given indexvoidsetInterval(T obj, float intervalSize)Set the interval size on the passed in object.intsize()Tvalue()Tvalue(float probability)Finds the value whose interval contains the given probability Binary search algorithm is used to find the value.
-
-
-
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
-
-