@quenty/randomutils 2.0.0 → 2.1.1-canary.238.2c4d310.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 +19 -0
- package/LICENSE.md +1 -1
- package/README.md +2 -2
- package/package.json +2 -2
- package/src/Shared/RandomUtils.lua +93 -17
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,25 @@
|
|
|
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
|
+
## [2.1.1-canary.238.2c4d310.0](https://github.com/Quenty/NevermoreEngine/compare/@quenty/randomutils@2.1.0...@quenty/randomutils@2.1.1-canary.238.2c4d310.0) (2021-12-29)
|
|
7
|
+
|
|
8
|
+
**Note:** Version bump only for package @quenty/randomutils
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
# [2.1.0](https://github.com/Quenty/NevermoreEngine/compare/@quenty/randomutils@2.0.0...@quenty/randomutils@2.1.0) (2021-10-02)
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
### Features
|
|
18
|
+
|
|
19
|
+
* Add RandomUtils.guassuanRandom() and RandomUtils.randomUnitVector3 ([f903846](https://github.com/Quenty/NevermoreEngine/commit/f90384651aedd4a79600b2825b996123441227c9))
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
|
|
6
25
|
# [2.0.0](https://github.com/Quenty/NevermoreEngine/compare/@quenty/randomutils@1.2.0...@quenty/randomutils@2.0.0) (2021-09-05)
|
|
7
26
|
|
|
8
27
|
|
package/LICENSE.md
CHANGED
package/README.md
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
## RandomUtils
|
|
2
2
|
<div align="center">
|
|
3
|
-
<a href="http://quenty.github.io/
|
|
3
|
+
<a href="http://quenty.github.io/NevermoreEngine/">
|
|
4
4
|
<img src="https://img.shields.io/badge/docs-website-green.svg" alt="Documentation" />
|
|
5
5
|
</a>
|
|
6
6
|
<a href="https://discord.gg/mhtGUS8">
|
|
7
|
-
<img src="https://img.shields.io/
|
|
7
|
+
<img src="https://img.shields.io/discord/385151591524597761?color=5865F2&label=discord&logo=discord&logoColor=white" alt="Discord" />
|
|
8
8
|
</a>
|
|
9
9
|
<a href="https://github.com/Quenty/NevermoreEngine/actions">
|
|
10
10
|
<img src="https://github.com/Quenty/NevermoreEngine/actions/workflows/build.yml/badge.svg" alt="Build and release status" />
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@quenty/randomutils",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.1.1-canary.238.2c4d310.0",
|
|
4
4
|
"description": "Quenty's RandomUtils, utility functions for Roblox",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"Roblox",
|
|
@@ -28,5 +28,5 @@
|
|
|
28
28
|
"publishConfig": {
|
|
29
29
|
"access": "public"
|
|
30
30
|
},
|
|
31
|
-
"gitHead": "
|
|
31
|
+
"gitHead": "2c4d310b84afd0570d89667dc5d4aa69a0ef304a"
|
|
32
32
|
}
|
|
@@ -1,9 +1,31 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
--[=[
|
|
2
|
+
Utility functions involving random variables. This is quite useful
|
|
3
|
+
for a variety of game mechanics.
|
|
4
|
+
|
|
5
|
+
:::tip
|
|
6
|
+
Each method generally takes a random object in as the last argument,
|
|
7
|
+
which can be used to seed the randomness. This is especially useful for
|
|
8
|
+
reproducting state in testing.
|
|
9
|
+
:::
|
|
10
|
+
|
|
11
|
+
@class RandomUtils
|
|
12
|
+
]=]
|
|
4
13
|
|
|
5
14
|
local RandomUtils = {}
|
|
6
15
|
|
|
16
|
+
--[=[
|
|
17
|
+
Picks an option from a list. Returns nil if the list is empty.
|
|
18
|
+
|
|
19
|
+
```lua
|
|
20
|
+
local options = Players:GetPlayers()
|
|
21
|
+
local choosen = RandomUtils.choice(options)
|
|
22
|
+
print(choosen)
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
@param list { T }
|
|
26
|
+
@param random Random? -- Optional
|
|
27
|
+
@return T?
|
|
28
|
+
]=]
|
|
7
29
|
function RandomUtils.choice(list, random)
|
|
8
30
|
if #list == 0 then
|
|
9
31
|
return nil
|
|
@@ -18,34 +40,58 @@ function RandomUtils.choice(list, random)
|
|
|
18
40
|
end
|
|
19
41
|
end
|
|
20
42
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
43
|
+
--[=[
|
|
44
|
+
Creates a copy of the table, but shuffled using fisher-yates shuffle
|
|
45
|
+
|
|
46
|
+
@param list { T } -- A new table to copy
|
|
47
|
+
@param random Random? -- Optional random to use when shuffling
|
|
48
|
+
@return { T }
|
|
49
|
+
]=]
|
|
50
|
+
function RandomUtils.shuffledCopy(list, random)
|
|
51
|
+
local copy = {}
|
|
52
|
+
for i=1, #list do
|
|
53
|
+
copy[i] = list[i]
|
|
28
54
|
end
|
|
29
55
|
|
|
30
|
-
RandomUtils.shuffle(
|
|
56
|
+
RandomUtils.shuffle(copy, random)
|
|
31
57
|
|
|
32
|
-
return
|
|
58
|
+
return copy
|
|
33
59
|
end
|
|
34
60
|
|
|
35
|
-
|
|
61
|
+
--[=[
|
|
62
|
+
Shuffles the list in place
|
|
63
|
+
|
|
64
|
+
@param list {T}
|
|
65
|
+
@param random Random? -- Optional random to use when shuffling
|
|
66
|
+
]=]
|
|
67
|
+
function RandomUtils.shuffle(list, random)
|
|
36
68
|
if random then
|
|
37
|
-
for i = #
|
|
69
|
+
for i = #list, 2, -1 do
|
|
38
70
|
local j = random:NextInteger(1, i)
|
|
39
|
-
|
|
71
|
+
list[i], list[j] = list[j], list[i]
|
|
40
72
|
end
|
|
41
73
|
else
|
|
42
|
-
for i = #
|
|
74
|
+
for i = #list, 2, -1 do
|
|
43
75
|
local j = math.random(i)
|
|
44
|
-
|
|
76
|
+
list[i], list[j] = list[j], list[i]
|
|
45
77
|
end
|
|
46
78
|
end
|
|
47
79
|
end
|
|
48
80
|
|
|
81
|
+
--[=[
|
|
82
|
+
Like [/api/RandomUtils#Choice](RandomUtils.Choice) but weighted options in a
|
|
83
|
+
performance friendly way. Takes O(n) time.
|
|
84
|
+
|
|
85
|
+
:::warning
|
|
86
|
+
A weight of 0 may still be picked, and negative weights may result in
|
|
87
|
+
undefined behavior.
|
|
88
|
+
:::
|
|
89
|
+
|
|
90
|
+
@param list { T } -- List of options
|
|
91
|
+
@param weights { number } -- Array the same length with weights.
|
|
92
|
+
@param random Random? -- Optional random
|
|
93
|
+
@return T? -- May return nil if the list is empty
|
|
94
|
+
]=]
|
|
49
95
|
function RandomUtils.weightedChoice(list, weights, random)
|
|
50
96
|
if #list == 0 then
|
|
51
97
|
return nil
|
|
@@ -81,4 +127,34 @@ function RandomUtils.weightedChoice(list, weights, random)
|
|
|
81
127
|
end
|
|
82
128
|
end
|
|
83
129
|
|
|
130
|
+
--[=[
|
|
131
|
+
Computes the gaussian random function which is the independent probability curve.
|
|
132
|
+
|
|
133
|
+
@param random Random? -- Optional random to use
|
|
134
|
+
@return number
|
|
135
|
+
]=]
|
|
136
|
+
function RandomUtils.gaussianRandom(random)
|
|
137
|
+
local a, t
|
|
138
|
+
if random then
|
|
139
|
+
a = 2*math.pi*random:NextNumber()
|
|
140
|
+
t = random:NextNumber()
|
|
141
|
+
else
|
|
142
|
+
a = 2*math.pi*math.random()
|
|
143
|
+
t = math.random()
|
|
144
|
+
end
|
|
145
|
+
|
|
146
|
+
return math.sqrt(-2*math.log(1 - t))*math.cos(a)
|
|
147
|
+
end
|
|
148
|
+
|
|
149
|
+
--[=[
|
|
150
|
+
@param random? Random? -- Optional random to use
|
|
151
|
+
@return Vector3
|
|
152
|
+
]=]
|
|
153
|
+
function RandomUtils.randomUnitVector3(random)
|
|
154
|
+
return Vector3.new(
|
|
155
|
+
RandomUtils.gaussianRandom(random),
|
|
156
|
+
RandomUtils.gaussianRandom(random),
|
|
157
|
+
RandomUtils.gaussianRandom(random))
|
|
158
|
+
end
|
|
159
|
+
|
|
84
160
|
return RandomUtils
|