BGC Tools
Static Public Member Functions
BGC.Mathematics.Probability Class Reference

Static Public Member Functions

static int GetIndexWIthPreviousLowerProbability (float arrayLength, int previousIndex)
 Get a random index from an array where the previous has a lower probability More...
 
static int GetRandomIndexBasedOnWeights (float[] weights, bool verbose=false)
 Given an array of weights for each index of the array, return a random index based on said weights. More...
 

Detailed Description

Definition at line 6 of file Probability.cs.

Member Function Documentation

◆ 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.

13  {
14  int index = -1;
15  if (arrayLength <= 0)
16  {
17  return index;
18  }
19 
20  float regularProbability = 1f / arrayLength;
21  float highProbability = 3f / (2f * arrayLength);
22  float lowProbability = 1f / (2f * arrayLength);
23  float lowSelectProbability = 1f / (arrayLength - 1f);
24 
25  float requriedProbForHighSelect = Random.value;
26  float requiredProbForLowSelect = Random.value;
27 
28  float cumulativeHighProbSelect = 0f;
29  float cumulativeLowProbSelect = 0f;
30 
31  bool lowProbabilitySelected = false;
32 
33  for (int i = 0; i < arrayLength; ++i)
34  {
35  if (i == previousIndex)
36  {
37  cumulativeHighProbSelect += lowProbability;
38  }
39  else
40  {
41  cumulativeLowProbSelect += lowSelectProbability;
42 
43  if (lowProbabilitySelected && cumulativeLowProbSelect > requiredProbForLowSelect)
44  {
45  cumulativeHighProbSelect += highProbability;
46  }
47  else
48  {
49  cumulativeHighProbSelect += regularProbability;
50  }
51  }
52 
53  if (cumulativeHighProbSelect > requriedProbForHighSelect)
54  {
55  index = i;
56  break;
57  }
58  }
59 
60  if (index == -1)
61  {
62  index = ((int) arrayLength) - 1;
63  }
64 
65  return index;
66  }

◆ 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.

73  {
74  if (weights == null || weights.Length <= 0)
75  {
76  if (verbose && weights == null)
77  {
78  Debug.LogError("Array is null and this function returned -1.");
79  }
80  else if (verbose)
81  {
82  Debug.LogError("Array is <= 0 and this function returned -1.");
83  }
84 
85  return -1;
86  }
87 
88  int index = 0;
89  float total = 0;
90 
91  for (int i = 0; i < weights.Length; ++i)
92  {
93  total += weights[i];
94  }
95 
96  if (total <= 0)
97  {
98  index = weights.RandomIndex();
99  }
100  else
101  {
102  float x = 1f / total;
103 
104  float random = Random.value;
105  float cumulative = weights[0] * x;
106 
107  while (cumulative < random)
108  {
109  ++index;
110  cumulative += weights[index] * x;
111  }
112  }
113 
114  return index;
115  }

The documentation for this class was generated from the following file: