ransyr 1.2.0 → 1.2.1
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/main.cjs +51 -0
- package/package.json +4 -2
package/main.cjs
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
module.exports = class ransyr {
|
|
2
|
+
|
|
3
|
+
static random() {
|
|
4
|
+
return Math.random();
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
static sample(arr, k) {
|
|
8
|
+
if (k > arr.length) {
|
|
9
|
+
throw new RangeError("sample size larger than population");
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const copy = [...arr];
|
|
13
|
+
const result = [];
|
|
14
|
+
|
|
15
|
+
for (let i = 0; i < k; i++) {
|
|
16
|
+
const idx = Math.floor(Math.random() * copy.length);
|
|
17
|
+
result.push(copy[idx]);
|
|
18
|
+
copy.splice(idx, 1);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
return result;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
static randint(min, max) {
|
|
25
|
+
if (min > max) [min,
|
|
26
|
+
max] = [max,
|
|
27
|
+
min];
|
|
28
|
+
return Math.floor(Math.random() * (max - min + 1)) + min;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
static choice(arr) {
|
|
32
|
+
if (!arr.length) return undefined;
|
|
33
|
+
return arr[Math.floor(Math.random() * arr.length)];
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
static shuffle(arr) {
|
|
37
|
+
const copy = [...arr];
|
|
38
|
+
for (let i = copy.length - 1; i > 0; i--) {
|
|
39
|
+
const j = Math.floor(Math.random() * (i + 1));
|
|
40
|
+
[copy[i],
|
|
41
|
+
copy[j]] = [copy[j],
|
|
42
|
+
copy[i]];
|
|
43
|
+
}
|
|
44
|
+
return copy;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
static chance(percent) {
|
|
48
|
+
return Math.random() * 100 < percent;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
package/package.json
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ransyr",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.1",
|
|
4
4
|
"description": "python like random utilities for make things easier",
|
|
5
|
-
"main": "main.
|
|
5
|
+
"main": "main.cjs",
|
|
6
|
+
"module": "main.js",
|
|
7
|
+
"type": "module",
|
|
6
8
|
"scripts": {
|
|
7
9
|
"test": "echo \"Error: no test specified\" && exit 1"
|
|
8
10
|
},
|