math-utils-simple 4.0.0 → 4.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/package.json +10 -2
- package/src/index.js +22 -1
- package/src/random/random.js +3 -0
- package/src/random/randomBoolean.js +3 -0
- package/src/random/randomChoice.js +11 -0
- package/src/random/randomFloat.js +11 -0
- package/src/random/randomInt.js +11 -0
- package/src/random/sample.js +17 -0
- package/src/random/shuffle.js +15 -0
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "math-utils-simple",
|
|
3
|
-
"version": "4.0.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "4.0.1",
|
|
4
|
+
"description": "Math utility library for npm",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./src/index.js",
|
|
7
7
|
"exports": {
|
|
@@ -16,6 +16,14 @@
|
|
|
16
16
|
"npm",
|
|
17
17
|
"library"
|
|
18
18
|
],
|
|
19
|
+
"repository": {
|
|
20
|
+
"type": "git",
|
|
21
|
+
"url": "git+https://github.com/umar-farooq-5757/math-utils-simple.git"
|
|
22
|
+
},
|
|
23
|
+
"bugs": {
|
|
24
|
+
"url": "https://github.com/umar-farooq-5757/math-utils-simple/issues"
|
|
25
|
+
},
|
|
26
|
+
"homepage": "https://github.com/umar-farooq-5757/math-utils-simple#readme",
|
|
19
27
|
"author": "Umar Farooq",
|
|
20
28
|
"license": "MIT"
|
|
21
29
|
}
|
package/src/index.js
CHANGED
|
@@ -104,4 +104,25 @@ export { default as projectVector } from "./arrays/projectVector.js";
|
|
|
104
104
|
export { default as cumulativeSum } from "./arrays/cumulativeSum.js";
|
|
105
105
|
export { default as movingAverage } from "./arrays/movingAverage.js";
|
|
106
106
|
export { default as transpose } from "./arrays/transpose.js";
|
|
107
|
-
export { default as matrixMultiply } from "./arrays/matrixMultiply.js";
|
|
107
|
+
export { default as matrixMultiply } from "./arrays/matrixMultiply.js";
|
|
108
|
+
|
|
109
|
+
// RANDOM
|
|
110
|
+
export { default as random } from "./random/random.js";
|
|
111
|
+
export { default as randomInt } from "./random/randomInt.js";
|
|
112
|
+
export { default as randomFloat } from "./random/randomFloat.js";
|
|
113
|
+
export { default as randomBoolean } from "./random/randomBoolean.js";
|
|
114
|
+
export { default as randomChoice } from "./random/randomChoice.js";
|
|
115
|
+
export { default as shuffle } from "./random/shuffle.js";
|
|
116
|
+
export { default as sample } from "./random/sample.js";
|
|
117
|
+
/**
|
|
118
|
+
* future goals
|
|
119
|
+
* randomSign() → returns -1 or 1
|
|
120
|
+
randomHexColor()
|
|
121
|
+
randomRGB()
|
|
122
|
+
randomUUID() (using crypto.randomUUID())
|
|
123
|
+
randomString(length, charset)
|
|
124
|
+
randomDate(start, end)
|
|
125
|
+
weightedChoice(items, weights)
|
|
126
|
+
randomGaussian(mean, stdDev) (normal distribution via the Box–Muller transform)
|
|
127
|
+
randomSeed(seed) (seeded pseudo-random number generator)
|
|
128
|
+
*/
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export default function randomChoice(array) {
|
|
2
|
+
if (!Array.isArray(array)) {
|
|
3
|
+
throw new TypeError("Expected an array.");
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
if (array.length === 0) {
|
|
7
|
+
throw new Error("Cannot choose from an empty array.");
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
return array[Math.floor(Math.random() * array.length)];
|
|
11
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export default function randomFloat(min, max) {
|
|
2
|
+
if (typeof min !== "number" || typeof max !== "number") {
|
|
3
|
+
throw new TypeError("Expected numeric bounds.");
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
if (min > max) {
|
|
7
|
+
throw new Error("Minimum cannot be greater than maximum.");
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
return Math.random() * (max - min) + min;
|
|
11
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export default function randomInt(min, max) {
|
|
2
|
+
if (!Number.isInteger(min) || !Number.isInteger(max)) {
|
|
3
|
+
throw new TypeError("Expected integer bounds.");
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
if (min > max) {
|
|
7
|
+
throw new Error("Minimum cannot be greater than maximum.");
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
return Math.floor(Math.random() * (max - min + 1)) + min;
|
|
11
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import shuffle from "./shuffle.js";
|
|
2
|
+
|
|
3
|
+
export default function sample(array, count) {
|
|
4
|
+
if (!Array.isArray(array)) {
|
|
5
|
+
throw new TypeError("Expected an array.");
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
if (!Number.isInteger(count) || count < 0) {
|
|
9
|
+
throw new Error("Count must be a non-negative integer.");
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
if (count > array.length) {
|
|
13
|
+
throw new Error("Count cannot exceed array length.");
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
return shuffle(array).slice(0, count);
|
|
17
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export default function shuffle(array) {
|
|
2
|
+
if (!Array.isArray(array)) {
|
|
3
|
+
throw new TypeError("Expected an array.");
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
const result = [...array];
|
|
7
|
+
|
|
8
|
+
for (let i = result.length - 1; i > 0; i--) {
|
|
9
|
+
const j = Math.floor(Math.random() * (i + 1));
|
|
10
|
+
|
|
11
|
+
[result[i], result[j]] = [result[j], result[i]];
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
return result;
|
|
15
|
+
}
|