@quenty/randomutils 6.10.2 → 6.10.3
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 +8 -0
- package/package.json +4 -4
- package/src/Shared/RandomSampler.lua +16 -6
- package/src/Shared/WeightedRandomChooser.lua +26 -14
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,14 @@
|
|
|
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
|
+
## [6.10.3](https://github.com/Quenty/NevermoreEngine/compare/@quenty/randomutils@6.10.2...@quenty/randomutils@6.10.3) (2025-04-10)
|
|
7
|
+
|
|
8
|
+
**Note:** Version bump only for package @quenty/randomutils
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
|
|
6
14
|
## [6.10.2](https://github.com/Quenty/NevermoreEngine/compare/@quenty/randomutils@6.10.0...@quenty/randomutils@6.10.2) (2025-04-07)
|
|
7
15
|
|
|
8
16
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@quenty/randomutils",
|
|
3
|
-
"version": "6.10.
|
|
3
|
+
"version": "6.10.3",
|
|
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": "^10.8.
|
|
33
|
-
"@quenty/table": "^3.7.
|
|
32
|
+
"@quenty/loader": "^10.8.3",
|
|
33
|
+
"@quenty/table": "^3.7.4"
|
|
34
34
|
},
|
|
35
|
-
"gitHead": "
|
|
35
|
+
"gitHead": "b06c070ae91d5dab7bd8de6e290ad2caabb15d8f"
|
|
36
36
|
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
--!strict
|
|
1
2
|
--[=[
|
|
2
3
|
@class RandomSampler
|
|
3
4
|
]=]
|
|
@@ -10,14 +11,23 @@ local RandomSampler = {}
|
|
|
10
11
|
RandomSampler.ClassName = "RandomSampler"
|
|
11
12
|
RandomSampler.__index = RandomSampler
|
|
12
13
|
|
|
14
|
+
export type RandomSampler<T> = typeof(setmetatable(
|
|
15
|
+
{} :: {
|
|
16
|
+
_optionsList: { T },
|
|
17
|
+
_shuffledAvailableList: { T },
|
|
18
|
+
_lastSelection: T?,
|
|
19
|
+
},
|
|
20
|
+
{} :: typeof({ __index = RandomSampler })
|
|
21
|
+
))
|
|
22
|
+
|
|
13
23
|
--[=[
|
|
14
24
|
Constructs a new RandomSampler
|
|
15
25
|
|
|
16
26
|
@param samples { T } -- The list of samples to sample from
|
|
17
27
|
@return RandomSampler<T>
|
|
18
28
|
]=]
|
|
19
|
-
function RandomSampler.new(samples)
|
|
20
|
-
local self = setmetatable({}, RandomSampler)
|
|
29
|
+
function RandomSampler.new<T>(samples): RandomSampler<T>
|
|
30
|
+
local self: RandomSampler<T> = setmetatable({} :: any, RandomSampler)
|
|
21
31
|
|
|
22
32
|
self._optionsList = {}
|
|
23
33
|
self._shuffledAvailableList = {}
|
|
@@ -35,7 +45,7 @@ end
|
|
|
35
45
|
|
|
36
46
|
@param samples { T } -- The list of samples to sample from
|
|
37
47
|
]=]
|
|
38
|
-
function RandomSampler
|
|
48
|
+
function RandomSampler.SetSamples<T>(self: RandomSampler<T>, samples: { T })
|
|
39
49
|
assert(type(samples) == "table", "Bad samples")
|
|
40
50
|
|
|
41
51
|
if self._optionsList ~= samples then
|
|
@@ -51,12 +61,12 @@ end
|
|
|
51
61
|
|
|
52
62
|
@return T -- The sample
|
|
53
63
|
]=]
|
|
54
|
-
function RandomSampler
|
|
64
|
+
function RandomSampler.Sample<T>(self: RandomSampler<T>): T
|
|
55
65
|
if #self._shuffledAvailableList == 0 then
|
|
56
66
|
self:Refill()
|
|
57
67
|
end
|
|
58
68
|
|
|
59
|
-
local selection = table.remove(self._shuffledAvailableList)
|
|
69
|
+
local selection: T = table.remove(self._shuffledAvailableList) :: any
|
|
60
70
|
self._lastSelection = selection
|
|
61
71
|
|
|
62
72
|
return selection
|
|
@@ -65,7 +75,7 @@ end
|
|
|
65
75
|
--[=[
|
|
66
76
|
Refills the list
|
|
67
77
|
]=]
|
|
68
|
-
function RandomSampler
|
|
78
|
+
function RandomSampler.Refill<T>(self: RandomSampler<T>)
|
|
69
79
|
local newList = RandomUtils.shuffledCopy(self._optionsList)
|
|
70
80
|
|
|
71
81
|
if #newList > 1 then
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
--!strict
|
|
1
2
|
--[=[
|
|
2
3
|
@class WeightedRandomChooser
|
|
3
4
|
]=]
|
|
@@ -10,13 +11,23 @@ local WeightedRandomChooser = {}
|
|
|
10
11
|
WeightedRandomChooser.ClassName = "WeightedRandomChooser"
|
|
11
12
|
WeightedRandomChooser.__index = WeightedRandomChooser
|
|
12
13
|
|
|
14
|
+
type WeightedRandomChooserCache<T> = { options: { T }, weights: { number }, total: number }
|
|
15
|
+
|
|
16
|
+
export type WeightedRandomChooser<T> = typeof(setmetatable(
|
|
17
|
+
{} :: {
|
|
18
|
+
_optionToWeight: { [T]: number },
|
|
19
|
+
_cache: WeightedRandomChooserCache<T>?,
|
|
20
|
+
},
|
|
21
|
+
{} :: typeof({ __index = WeightedRandomChooser })
|
|
22
|
+
))
|
|
23
|
+
|
|
13
24
|
--[=[
|
|
14
25
|
Creates a new weighted random chooser
|
|
15
26
|
|
|
16
27
|
@return WeightedRandomChooser<T>
|
|
17
28
|
]=]
|
|
18
|
-
function WeightedRandomChooser.new()
|
|
19
|
-
local self = setmetatable({}, WeightedRandomChooser)
|
|
29
|
+
function WeightedRandomChooser.new<T>(): WeightedRandomChooser<T>
|
|
30
|
+
local self: WeightedRandomChooser<T> = setmetatable({} :: any, WeightedRandomChooser)
|
|
20
31
|
|
|
21
32
|
self._optionToWeight = {}
|
|
22
33
|
|
|
@@ -28,9 +39,9 @@ end
|
|
|
28
39
|
removes the option.
|
|
29
40
|
|
|
30
41
|
@param option T
|
|
31
|
-
@param weight number
|
|
42
|
+
@param weight number | nil
|
|
32
43
|
]=]
|
|
33
|
-
function WeightedRandomChooser
|
|
44
|
+
function WeightedRandomChooser.SetWeight<T>(self: WeightedRandomChooser<T>, option: T, weight: number | nil)
|
|
34
45
|
assert(option ~= nil, "Bad option")
|
|
35
46
|
assert(type(weight) == "number" or weight == nil, "Bad weight")
|
|
36
47
|
|
|
@@ -39,7 +50,7 @@ function WeightedRandomChooser:SetWeight(option, weight)
|
|
|
39
50
|
end
|
|
40
51
|
|
|
41
52
|
self._cache = nil
|
|
42
|
-
self._optionToWeight[option] = weight
|
|
53
|
+
self._optionToWeight[option] = weight :: any
|
|
43
54
|
end
|
|
44
55
|
|
|
45
56
|
--[=[
|
|
@@ -47,7 +58,7 @@ end
|
|
|
47
58
|
|
|
48
59
|
@param option T
|
|
49
60
|
]=]
|
|
50
|
-
function WeightedRandomChooser
|
|
61
|
+
function WeightedRandomChooser.Remove<T>(self: WeightedRandomChooser<T>, option: T)
|
|
51
62
|
self:SetWeight(option, nil)
|
|
52
63
|
end
|
|
53
64
|
|
|
@@ -57,7 +68,7 @@ end
|
|
|
57
68
|
@param option T
|
|
58
69
|
@return number?
|
|
59
70
|
]=]
|
|
60
|
-
function WeightedRandomChooser
|
|
71
|
+
function WeightedRandomChooser.GetWeight<T>(self: WeightedRandomChooser<T>, option): number?
|
|
61
72
|
return self._optionToWeight[option]
|
|
62
73
|
end
|
|
63
74
|
|
|
@@ -67,9 +78,9 @@ end
|
|
|
67
78
|
@param option T
|
|
68
79
|
@return number?
|
|
69
80
|
]=]
|
|
70
|
-
function WeightedRandomChooser
|
|
81
|
+
function WeightedRandomChooser.GetProbability<T>(self: WeightedRandomChooser<T>, option): number?
|
|
71
82
|
local weight = self._optionToWeight[option]
|
|
72
|
-
if weight then
|
|
83
|
+
if not weight then
|
|
73
84
|
return nil
|
|
74
85
|
end
|
|
75
86
|
|
|
@@ -83,7 +94,7 @@ end
|
|
|
83
94
|
@param random Random
|
|
84
95
|
@return T
|
|
85
96
|
]=]
|
|
86
|
-
function WeightedRandomChooser
|
|
97
|
+
function WeightedRandomChooser.Choose<T>(self: WeightedRandomChooser<T>, random: Random?)
|
|
87
98
|
local data = self:_getOrCreateDataCache()
|
|
88
99
|
|
|
89
100
|
local randomNum
|
|
@@ -110,7 +121,7 @@ function WeightedRandomChooser:Choose(random: Random?)
|
|
|
110
121
|
return data.options[#data.options]
|
|
111
122
|
end
|
|
112
123
|
|
|
113
|
-
function WeightedRandomChooser
|
|
124
|
+
function WeightedRandomChooser._getOrCreateDataCache<T>(self: WeightedRandomChooser<T>): WeightedRandomChooserCache<T>
|
|
114
125
|
if self._cache then
|
|
115
126
|
return self._cache
|
|
116
127
|
end
|
|
@@ -121,16 +132,17 @@ function WeightedRandomChooser:_getOrCreateDataCache()
|
|
|
121
132
|
local total = 0
|
|
122
133
|
for index, key in options do
|
|
123
134
|
local weight = self._optionToWeight[key]
|
|
124
|
-
total
|
|
135
|
+
total += weight
|
|
125
136
|
weights[index] = weight
|
|
126
137
|
end
|
|
127
138
|
|
|
128
|
-
|
|
139
|
+
local cache: WeightedRandomChooserCache<T> = {
|
|
129
140
|
options = options,
|
|
130
141
|
weights = weights,
|
|
131
142
|
total = total,
|
|
132
143
|
}
|
|
133
|
-
|
|
144
|
+
self._cache = cache
|
|
145
|
+
return cache
|
|
134
146
|
end
|
|
135
147
|
|
|
136
148
|
return WeightedRandomChooser
|