@quenty/randomutils 2.3.0 → 3.0.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 +12 -0
- package/package.json +4 -3
- package/src/Shared/WeightedRandomChooser.lua +96 -0
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,18 @@
|
|
|
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.0.0](https://github.com/Quenty/NevermoreEngine/compare/@quenty/randomutils@2.3.0...@quenty/randomutils@3.0.0) (2023-10-11)
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
### Features
|
|
10
|
+
|
|
11
|
+
* Add GetProbability ([2267080](https://github.com/Quenty/NevermoreEngine/commit/2267080fc6c1c53153f15cd9f638bddbff2371b7))
|
|
12
|
+
* Add WeightedRandomChooser ([b0cdcd1](https://github.com/Quenty/NevermoreEngine/commit/b0cdcd1ab69104535683609469cd806e647ef000))
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
|
|
6
18
|
# [2.3.0](https://github.com/Quenty/NevermoreEngine/compare/@quenty/randomutils@2.2.0...@quenty/randomutils@2.3.0) (2023-08-23)
|
|
7
19
|
|
|
8
20
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@quenty/randomutils",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.0",
|
|
4
4
|
"description": "Quenty's RandomUtils, utility functions for Roblox",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"Roblox",
|
|
@@ -29,7 +29,8 @@
|
|
|
29
29
|
"access": "public"
|
|
30
30
|
},
|
|
31
31
|
"dependencies": {
|
|
32
|
-
"@quenty/loader": "^
|
|
32
|
+
"@quenty/loader": "^7.0.0",
|
|
33
|
+
"@quenty/table": "^3.3.0"
|
|
33
34
|
},
|
|
34
|
-
"gitHead": "
|
|
35
|
+
"gitHead": "fdeae46099587019ec5fc15317dc673aed379400"
|
|
35
36
|
}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
--[=[
|
|
2
|
+
@class WeightedRandomChooser
|
|
3
|
+
]=]
|
|
4
|
+
|
|
5
|
+
local require = require(script.Parent.loader).load(script)
|
|
6
|
+
|
|
7
|
+
local RandomUtils = require("RandomUtils")
|
|
8
|
+
local Table = require("Table")
|
|
9
|
+
|
|
10
|
+
local WeightedRandomChooser = {}
|
|
11
|
+
WeightedRandomChooser.ClassName = "WeightedRandomChooser"
|
|
12
|
+
WeightedRandomChooser.__index = WeightedRandomChooser
|
|
13
|
+
|
|
14
|
+
--[=[
|
|
15
|
+
Creates a new weighted random chooser
|
|
16
|
+
|
|
17
|
+
@return WeightedRandomChooser<T>
|
|
18
|
+
]=]
|
|
19
|
+
function WeightedRandomChooser.new()
|
|
20
|
+
local self = setmetatable({}, WeightedRandomChooser)
|
|
21
|
+
|
|
22
|
+
self._optionToWeight = {}
|
|
23
|
+
|
|
24
|
+
return self
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
--[=[
|
|
28
|
+
Sets the weight for a given option. Setting the weight to nil
|
|
29
|
+
removes the option.
|
|
30
|
+
|
|
31
|
+
@param option T
|
|
32
|
+
@param weight number | nil
|
|
33
|
+
]=]
|
|
34
|
+
function WeightedRandomChooser:SetWeight(option, weight)
|
|
35
|
+
assert(option ~= nil, "Bad option")
|
|
36
|
+
assert(type(weight) == "number" or weight == nil, "Bad weight")
|
|
37
|
+
|
|
38
|
+
self._optionToWeight[option] = weight
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
--[=[
|
|
42
|
+
Gets the weight for the option
|
|
43
|
+
|
|
44
|
+
@param option T
|
|
45
|
+
@return number | nil
|
|
46
|
+
]=]
|
|
47
|
+
function WeightedRandomChooser:GetWeight(option)
|
|
48
|
+
return self._optionToWeight[option]
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
--[=[
|
|
52
|
+
Gets the percent probability from 0 to 1
|
|
53
|
+
|
|
54
|
+
@param option T
|
|
55
|
+
@return number | nil
|
|
56
|
+
]=]
|
|
57
|
+
function WeightedRandomChooser:GetProbability(option)
|
|
58
|
+
local weight = self._optionToWeight[option]
|
|
59
|
+
if weight then
|
|
60
|
+
return nil
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
-- TODO: Cache if we call like a million times
|
|
64
|
+
local total = 0
|
|
65
|
+
for _, item in pairs(self._optionToWeight) do
|
|
66
|
+
total = total + item
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
return weight/total
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
--[=[
|
|
73
|
+
Removes the option from the chooser. Equivalent of setting the weight to nil
|
|
74
|
+
|
|
75
|
+
@param option T
|
|
76
|
+
]=]
|
|
77
|
+
function WeightedRandomChooser:Remove(option)
|
|
78
|
+
self:SetWeight(option, nil)
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
--[=[
|
|
82
|
+
Picks a weighted choise
|
|
83
|
+
|
|
84
|
+
@return T
|
|
85
|
+
]=]
|
|
86
|
+
function WeightedRandomChooser:Choose()
|
|
87
|
+
local options = Table.keys(self._optionToWeight)
|
|
88
|
+
local weights = {}
|
|
89
|
+
for index, key in pairs(options) do
|
|
90
|
+
weights[index] = self._optionToWeight[key]
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
return RandomUtils.weightedChoice(options, weights)
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
return WeightedRandomChooser
|