Definition at line 6 of file Probability.cs.
◆ GetIndexWIthPreviousLowerProbability()
static int BGC.Mathematics.Probability.GetIndexWIthPreviousLowerProbability |
( |
float |
arrayLength, |
|
|
int |
previousIndex |
|
) |
| |
|
inlinestatic |
Get a random index from an array where the previous has a lower probability
Definition at line 12 of file Probability.cs.
20 float regularProbability = 1f / arrayLength;
21 float highProbability = 3f / (2f * arrayLength);
22 float lowProbability = 1f / (2f * arrayLength);
23 float lowSelectProbability = 1f / (arrayLength - 1f);
25 float requriedProbForHighSelect = Random.value;
26 float requiredProbForLowSelect = Random.value;
28 float cumulativeHighProbSelect = 0f;
29 float cumulativeLowProbSelect = 0f;
31 bool lowProbabilitySelected =
false;
33 for (
int i = 0; i < arrayLength; ++i)
35 if (i == previousIndex)
37 cumulativeHighProbSelect += lowProbability;
41 cumulativeLowProbSelect += lowSelectProbability;
43 if (lowProbabilitySelected && cumulativeLowProbSelect > requiredProbForLowSelect)
45 cumulativeHighProbSelect += highProbability;
49 cumulativeHighProbSelect += regularProbability;
53 if (cumulativeHighProbSelect > requriedProbForHighSelect)
62 index = ((int) arrayLength) - 1;
◆ GetRandomIndexBasedOnWeights()
static int BGC.Mathematics.Probability.GetRandomIndexBasedOnWeights |
( |
float [] |
weights, |
|
|
bool |
verbose = false |
|
) |
| |
|
inlinestatic |
Given an array of weights for each index of the array, return a random index based on said weights.
Definition at line 72 of file Probability.cs.
74 if (weights == null || weights.Length <= 0)
76 if (verbose && weights == null)
78 Debug.LogError(
"Array is null and this function returned -1.");
82 Debug.LogError(
"Array is <= 0 and this function returned -1.");
91 for (
int i = 0; i < weights.Length; ++i)
98 index = weights.RandomIndex();
102 float x = 1f / total;
104 float random = Random.value;
105 float cumulative = weights[0] * x;
107 while (cumulative < random)
110 cumulative += weights[index] * x;
The documentation for this class was generated from the following file: