/
RandomUtils.lua
194 lines (161 loc) · 4.24 KB
/
RandomUtils.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
--[=[
Utility functions involving random variables. This is quite useful
for a variety of game mechanics.
:::tip
Each method generally takes a random object in as the last argument,
which can be used to seed the randomness. This is especially useful for
reproducting state in testing.
:::
@class RandomUtils
]=]
local RandomUtils = {}
--[=[
Picks an option from a list. Returns nil if the list is empty.
```lua
local options = Players:GetPlayers()
local choice = RandomUtils.choice(options)
print(choice)
```
Deterministic version:
```lua
local options = { "apples", "oranges", "bananas" }
local random = Random.new()
print(RandomUtils.choice(options, random)) --> "apples"
```
@param list { T }
@param random Random? -- Optional
@return T?
]=]
function RandomUtils.choice(list, random)
if #list == 0 then
return nil
elseif #list == 1 then
return list[1]
else
if random then
return list[random:NextInteger(1, #list)]
else
return list[math.random(1, #list)]
end
end
end
--[=[
Creates a copy of the table, but shuffled using fisher-yates shuffle
```lua
local options = { "apples", "oranges", "bananas" }
local random = Random.new()
print(RandomUtils.shuffledCopy(options)) --> shuffled copy of table
print(RandomUtils.shuffledCopy(options, random)) --> deterministic shuffled copy of table
```
@param list { T } -- A new table to copy
@param random Random? -- Optional random to use when shuffling
@return { T }
]=]
function RandomUtils.shuffledCopy(list, random)
local copy = table.clone(list)
RandomUtils.shuffle(copy, random)
return copy
end
--[=[
Shuffles the list in place using fisher-yates shuffle.
```lua
local options = { "apples", "oranges", "bananas" }
local random = Random.new()
RandomUtils.shuffle(options, random)
print(options) --> deterministic shuffled copy of table
RandomUtils.shuffle(options)
print(options) --> shuffled table
```
@param list {T}
@param random Random? -- Optional random to use when shuffling
]=]
function RandomUtils.shuffle(list, random)
if random then
for i = #list, 2, -1 do
local j = random:NextInteger(1, i)
list[i], list[j] = list[j], list[i]
end
else
for i = #list, 2, -1 do
local j = math.random(i)
list[i], list[j] = list[j], list[i]
end
end
end
--[=[
Like [RandomUtils.choice] but weighted options in a
performance friendly way. Takes O(n) time.
:::warning
A weight of 0 may still be picked, and negative weights may result in
undefined behavior.
:::
```lua
local weights = { 1, 3, 10 }
local options = { "a", "b", "c" }
print(RandomUtils.weightedChoice(options, weights)) --> "c"
```
@param list { T } -- List of options
@param weights { number } -- Array the same length with weights.
@param random Random? -- Optional random
@return T? -- May return nil if the list is empty
]=]
function RandomUtils.weightedChoice(list, weights, random)
if #list == 0 then
return nil
elseif #list == 1 then
return list[1]
else
local total = 0
for i=1, #list do
assert(type(weights[i]) == "number", "Bad weights")
total = total + weights[i]
end
local randomNum
if random then
randomNum = random:NextNumber()
else
randomNum = math.random()
end
local totalSum = 0
for i=1, #list do
if weights[i] == 0 then
continue
end
totalSum = totalSum + weights[i]
local threshold = totalSum/total
if randomNum <= threshold then
return list[i]
end
end
-- we shouldn't get here, but if we do, pick the last one
warn("[RandomUtils.weightedChoice] - Failed to reach threshold! Algorithm is wrong!")
return list[#list]
end
end
--[=[
Computes the gaussian random function which is the independent probability curve.
@param random Random? -- Optional random to use
@return number
]=]
function RandomUtils.gaussianRandom(random)
local a, t
if random then
a = 2*math.pi*random:NextNumber()
t = random:NextNumber()
else
a = 2*math.pi*math.random()
t = math.random()
end
return math.sqrt(-2*math.log(1 - t))*math.cos(a)
end
--[=[
@param random? Random? -- Optional random to use
@return Vector3
]=]
function RandomUtils.randomUnitVector3(random)
return Vector3.new(
RandomUtils.gaussianRandom(random),
RandomUtils.gaussianRandom(random),
RandomUtils.gaussianRandom(random))
end
return RandomUtils