@esportsplus/random 0.0.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/.editorconfig +9 -0
- package/.gitattributes +2 -0
- package/build/index.d.ts +8 -0
- package/build/index.js +24 -0
- package/package.json +18 -0
- package/readme.md +2 -0
- package/src/index.ts +39 -0
- package/tsconfig.json +10 -0
package/.editorconfig
ADDED
package/.gitattributes
ADDED
package/build/index.d.ts
ADDED
package/build/index.js
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
let max = Math.pow(2, 32);
|
|
2
|
+
function browser() {
|
|
3
|
+
return window.crypto.getRandomValues(new Uint32Array(1))[0] / max;
|
|
4
|
+
}
|
|
5
|
+
function node() {
|
|
6
|
+
return require('crypto').randomBytes(4).readUInt32BE(0) / max;
|
|
7
|
+
}
|
|
8
|
+
const float = () => {
|
|
9
|
+
if (globalThis?.crypto) {
|
|
10
|
+
return browser();
|
|
11
|
+
}
|
|
12
|
+
return node();
|
|
13
|
+
};
|
|
14
|
+
const floats = (n) => {
|
|
15
|
+
let i, values = [];
|
|
16
|
+
while (--n > -1) {
|
|
17
|
+
i = Math.floor(Math.random() * (n + 1));
|
|
18
|
+
values[i] = values[n];
|
|
19
|
+
values[n] = float();
|
|
20
|
+
}
|
|
21
|
+
return values;
|
|
22
|
+
};
|
|
23
|
+
export default { float, floats };
|
|
24
|
+
export { float, floats };
|
package/package.json
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
{
|
|
2
|
+
"author": "ICJR",
|
|
3
|
+
"description": "Random",
|
|
4
|
+
"devDependencies": {
|
|
5
|
+
"@esportsplus/rspack": "^0.0.1"
|
|
6
|
+
},
|
|
7
|
+
"main": "build/index.js",
|
|
8
|
+
"name": "@esportsplus/random",
|
|
9
|
+
"private": false,
|
|
10
|
+
"scripts": {
|
|
11
|
+
"build": "tsc && tsc-alias",
|
|
12
|
+
"-": "-",
|
|
13
|
+
"prepare": "npm run build",
|
|
14
|
+
"prepublishOnly": "npm run build"
|
|
15
|
+
},
|
|
16
|
+
"types": "build/index.d.ts",
|
|
17
|
+
"version": "0.0.1"
|
|
18
|
+
}
|
package/readme.md
ADDED
package/src/index.ts
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
let max = Math.pow(2, 32);
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
function browser() {
|
|
5
|
+
return window.crypto.getRandomValues(new Uint32Array(1))[0] / max;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
function node() {
|
|
9
|
+
return require('crypto').randomBytes(4).readUInt32BE(0) / max;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
const float = (): number => {
|
|
14
|
+
if (globalThis?.crypto) {
|
|
15
|
+
return browser();
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
return node();
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
// Fisher-Yates shuffle
|
|
22
|
+
// - https://wikipedia.org/wiki/Fisher-Yates_shuffle
|
|
23
|
+
const floats = (n: number): number[] => {
|
|
24
|
+
let i: number,
|
|
25
|
+
values: number[] = [];
|
|
26
|
+
|
|
27
|
+
while(--n > -1) {
|
|
28
|
+
i = Math.floor(Math.random() * (n + 1));
|
|
29
|
+
|
|
30
|
+
values[i] = values[n];
|
|
31
|
+
values[n] = float();
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
return values;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
export default { float, floats };
|
|
39
|
+
export { float, floats };
|