Package arc.math
Class CumulativeDistribution<T>
java.lang.Object
arc.math.CumulativeDistribution<T>
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
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
Adds a value with interval size equal to zero to the distributionvoid
Adds a value with a given interval size to the distributionvoid
clear()
Removes all the values from the distributionvoid
generate()
Generate the cumulative distributionvoid
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 sizefloat
getInterval
(int index) getValue
(int index) void
setInterval
(int index, float intervalSize) Sets the interval size for the value at the given indexvoid
setInterval
(T obj, float intervalSize) Set the interval size on the passed in object.int
size()
value()
value
(float probability) Finds the value whose interval contains the given probability Binary search algorithm is used to find the value.
-
Constructor Details
-
CumulativeDistribution
public CumulativeDistribution()
-
-
Method Details
-
add
Adds a value with a given interval size to the distribution -
add
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
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
- 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
- Returns:
- the value at the given position
-
setInterval
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
-