@quenty/randomutils 2.1.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 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
+ ## [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
+
6
14
  # [2.1.0](https://github.com/Quenty/NevermoreEngine/compare/@quenty/randomutils@2.0.0...@quenty/randomutils@2.1.0) (2021-10-02)
7
15
 
8
16
 
package/LICENSE.md CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2014 Quenty
3
+ Copyright (c) 2014-2021 Quenty
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
package/README.md CHANGED
@@ -1,10 +1,10 @@
1
1
  ## RandomUtils
2
2
  <div align="center">
3
- <a href="http://quenty.github.io/api/">
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/badge/discord-nevermore-blue.svg" alt="Discord" />
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.1.0",
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": "2cae4b40a33765f444e6f0b1f3d6e181489dfa57"
31
+ "gitHead": "2c4d310b84afd0570d89667dc5d4aa69a0ef304a"
32
32
  }
@@ -1,9 +1,31 @@
1
- ---
2
- -- @module RandomUtils
3
- -- @author Quenty
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
- --- Creates a copy of the table, but shuffled using fisher-yates shuffle
22
- -- @tparam table orig A new table to copy
23
- -- @tparam[opt=nil] A random to use when shuffling
24
- function RandomUtils.shuffledCopy(orig, random)
25
- local tbl = {}
26
- for i=1, #orig do
27
- tbl[i] = orig[i]
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(tbl, random)
56
+ RandomUtils.shuffle(copy, random)
31
57
 
32
- return tbl
58
+ return copy
33
59
  end
34
60
 
35
- function RandomUtils.shuffle(tbl, random)
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 = #tbl, 2, -1 do
69
+ for i = #list, 2, -1 do
38
70
  local j = random:NextInteger(1, i)
39
- tbl[i], tbl[j] = tbl[j], tbl[i]
71
+ list[i], list[j] = list[j], list[i]
40
72
  end
41
73
  else
42
- for i = #tbl, 2, -1 do
74
+ for i = #list, 2, -1 do
43
75
  local j = math.random(i)
44
- tbl[i], tbl[j] = tbl[j], tbl[i]
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,6 +127,12 @@ 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
+ ]=]
84
136
  function RandomUtils.gaussianRandom(random)
85
137
  local a, t
86
138
  if random then
@@ -94,6 +146,10 @@ function RandomUtils.gaussianRandom(random)
94
146
  return math.sqrt(-2*math.log(1 - t))*math.cos(a)
95
147
  end
96
148
 
149
+ --[=[
150
+ @param random? Random? -- Optional random to use
151
+ @return Vector3
152
+ ]=]
97
153
  function RandomUtils.randomUnitVector3(random)
98
154
  return Vector3.new(
99
155
  RandomUtils.gaussianRandom(random),