@quenty/randomutils 3.0.0 → 3.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +11 -0
- package/package.json +4 -4
- package/src/Shared/WeightedRandomChooser.lua +59 -19
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,17 @@
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
5
|
|
|
6
|
+
# [3.1.0](https://github.com/Quenty/NevermoreEngine/compare/@quenty/randomutils@3.0.0...@quenty/randomutils@3.1.0) (2023-12-14)
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
### Features
|
|
10
|
+
|
|
11
|
+
* Cache random choice calculations to be speedy ([c32f301](https://github.com/Quenty/NevermoreEngine/commit/c32f30184d11e1ce04e3e9fa2cbd21861c54a09b))
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
6
17
|
# [3.0.0](https://github.com/Quenty/NevermoreEngine/compare/@quenty/randomutils@2.3.0...@quenty/randomutils@3.0.0) (2023-10-11)
|
|
7
18
|
|
|
8
19
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@quenty/randomutils",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.1.0",
|
|
4
4
|
"description": "Quenty's RandomUtils, utility functions for Roblox",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"Roblox",
|
|
@@ -29,8 +29,8 @@
|
|
|
29
29
|
"access": "public"
|
|
30
30
|
},
|
|
31
31
|
"dependencies": {
|
|
32
|
-
"@quenty/loader": "^7.
|
|
33
|
-
"@quenty/table": "^3.
|
|
32
|
+
"@quenty/loader": "^7.1.0",
|
|
33
|
+
"@quenty/table": "^3.4.0"
|
|
34
34
|
},
|
|
35
|
-
"gitHead": "
|
|
35
|
+
"gitHead": "2c2dbbc0cb2fbb46b4f3270c559c63890fe18b26"
|
|
36
36
|
}
|
|
@@ -4,7 +4,6 @@
|
|
|
4
4
|
|
|
5
5
|
local require = require(script.Parent.loader).load(script)
|
|
6
6
|
|
|
7
|
-
local RandomUtils = require("RandomUtils")
|
|
8
7
|
local Table = require("Table")
|
|
9
8
|
|
|
10
9
|
local WeightedRandomChooser = {}
|
|
@@ -35,9 +34,23 @@ function WeightedRandomChooser:SetWeight(option, weight)
|
|
|
35
34
|
assert(option ~= nil, "Bad option")
|
|
36
35
|
assert(type(weight) == "number" or weight == nil, "Bad weight")
|
|
37
36
|
|
|
37
|
+
if self._optionToWeight[option] == weight then
|
|
38
|
+
return
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
self._cache = nil
|
|
38
42
|
self._optionToWeight[option] = weight
|
|
39
43
|
end
|
|
40
44
|
|
|
45
|
+
--[=[
|
|
46
|
+
Removes the option from the chooser. Equivalent of setting the weight to nil
|
|
47
|
+
|
|
48
|
+
@param option T
|
|
49
|
+
]=]
|
|
50
|
+
function WeightedRandomChooser:Remove(option)
|
|
51
|
+
self:SetWeight(option, nil)
|
|
52
|
+
end
|
|
53
|
+
|
|
41
54
|
--[=[
|
|
42
55
|
Gets the weight for the option
|
|
43
56
|
|
|
@@ -60,37 +73,64 @@ function WeightedRandomChooser:GetProbability(option)
|
|
|
60
73
|
return nil
|
|
61
74
|
end
|
|
62
75
|
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
for _, item in pairs(self._optionToWeight) do
|
|
66
|
-
total = total + item
|
|
67
|
-
end
|
|
68
|
-
|
|
69
|
-
return weight/total
|
|
76
|
+
local cache = self:_getOrCreateDataCache()
|
|
77
|
+
return weight/cache.total
|
|
70
78
|
end
|
|
71
79
|
|
|
72
80
|
--[=[
|
|
73
|
-
|
|
81
|
+
Picks a weighted choise
|
|
74
82
|
|
|
75
|
-
@param
|
|
83
|
+
@param random Random
|
|
84
|
+
@return T
|
|
76
85
|
]=]
|
|
77
|
-
function WeightedRandomChooser:
|
|
78
|
-
self:
|
|
86
|
+
function WeightedRandomChooser:Choose(random)
|
|
87
|
+
local data = self:_getOrCreateDataCache()
|
|
88
|
+
|
|
89
|
+
local randomNum
|
|
90
|
+
if random then
|
|
91
|
+
randomNum = random:NextNumber()
|
|
92
|
+
else
|
|
93
|
+
randomNum = math.random()
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
local totalSum = 0
|
|
97
|
+
|
|
98
|
+
-- TODO: Binary search
|
|
99
|
+
for i=1, #data.options do
|
|
100
|
+
totalSum = totalSum + data.weights[i]
|
|
101
|
+
|
|
102
|
+
-- TODO: cache threshold?
|
|
103
|
+
local threshold = totalSum/data.total
|
|
104
|
+
if randomNum <= threshold then
|
|
105
|
+
return data.options[i]
|
|
106
|
+
end
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
warn("[WeightedRandomChooser.Choose] - Failed to reach threshold! Algorithm is wrong!")
|
|
110
|
+
return data.options[#data.options]
|
|
79
111
|
end
|
|
80
112
|
|
|
81
|
-
|
|
82
|
-
|
|
113
|
+
function WeightedRandomChooser:_getOrCreateDataCache()
|
|
114
|
+
if self._cache then
|
|
115
|
+
return self._cache
|
|
116
|
+
end
|
|
83
117
|
|
|
84
|
-
@return T
|
|
85
|
-
]=]
|
|
86
|
-
function WeightedRandomChooser:Choose()
|
|
87
118
|
local options = Table.keys(self._optionToWeight)
|
|
88
119
|
local weights = {}
|
|
120
|
+
|
|
121
|
+
local total = 0
|
|
89
122
|
for index, key in pairs(options) do
|
|
90
|
-
|
|
123
|
+
local weight = self._optionToWeight[key]
|
|
124
|
+
total = total + weight
|
|
125
|
+
weights[index] = weight
|
|
91
126
|
end
|
|
92
127
|
|
|
93
|
-
|
|
128
|
+
self._cache = {
|
|
129
|
+
options = options;
|
|
130
|
+
weights = weights;
|
|
131
|
+
total = total;
|
|
132
|
+
}
|
|
133
|
+
return self._cache
|
|
94
134
|
end
|
|
95
135
|
|
|
96
136
|
return WeightedRandomChooser
|