randomcryp 1.0.5 → 2.0.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/README.md +20 -19
- package/dist/index.d.ts +34 -129
- package/dist/index.js +23 -3
- package/dist/lib/bool.d.ts +5 -0
- package/dist/lib/boolean.d.ts +1 -0
- package/dist/lib/choice.d.ts +7 -0
- package/dist/lib/float.d.ts +5 -0
- package/dist/lib/hex.d.ts +7 -0
- package/dist/lib/ifloat.d.ts +7 -0
- package/dist/lib/percentage.d.ts +5 -0
- package/dist/lib/pick.d.ts +1 -0
- package/dist/lib/randInt.d.ts +1 -0
- package/dist/lib/random.d.ts +1 -0
- package/dist/lib/range.d.ts +8 -0
- package/dist/lib/rangeInt.d.ts +8 -0
- package/dist/lib/safeInt.d.ts +8 -0
- package/dist/lib/shuffle.d.ts +9 -0
- package/dist/lib/uSafeInt.d.ts +6 -0
- package/package.json +3 -2
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# randomcryp
|
|
2
2
|
|
|
3
|
-
A cryptographically secure, feature rich, zero dependency, lightweight and browser friendly random number generator library. Only ~
|
|
3
|
+
A cryptographically secure, feature rich, zero dependency, lightweight and browser friendly random number generator library. Only ~5KB in size.
|
|
4
4
|
Uses [Crypto.getRandomValues()](https://developer.mozilla.org/en-US/docs/Web/API/Crypto/getRandomValues) to get its randomness.
|
|
5
5
|
The spelling is random-creep in case you are wondering.
|
|
6
6
|
|
|
@@ -40,22 +40,23 @@ rangeInt(1, 10); // 7
|
|
|
40
40
|
|
|
41
41
|
#### List of Methods
|
|
42
42
|
|
|
43
|
-
| Method
|
|
44
|
-
|
|
|
45
|
-
| **`bool(): boolean`**
|
|
46
|
-
| **`boolean(): boolean`**
|
|
47
|
-
| **`percentage(val: number): boolean`**
|
|
48
|
-
| **`uSafeInt(): number`**
|
|
49
|
-
| **`float(): number`**
|
|
50
|
-
| **`random(): number`**
|
|
51
|
-
| **`
|
|
52
|
-
| **`
|
|
53
|
-
| **`
|
|
54
|
-
| **`
|
|
55
|
-
| **`
|
|
56
|
-
| **`
|
|
57
|
-
| **`
|
|
58
|
-
| **`
|
|
59
|
-
|
|
60
|
-
|
|
43
|
+
| Method | Description | Aliases |
|
|
44
|
+
| -- | -- | -- |
|
|
45
|
+
| **`bool(): boolean`** | Generates a random boolean (`true` or `false`). | `bool()` → `true` |
|
|
46
|
+
| **`boolean(): boolean`** | Alias for `bool()`. | `boolean()` → `false` |
|
|
47
|
+
| **`percentage(val: number): boolean`** | Generates `true` at given percentage of time. | `percentage(20)` → `false` |
|
|
48
|
+
| **`uSafeInt(): number`** | Generates a random integer between `0` (inclusive) and `Number.MAX_SAFE_INTEGER` (inclusive). | `uSafeInt()` → `4946544243668033` |
|
|
49
|
+
| **`float(): number`** | Generates a random number between `0` (inclusive) and `1` (exclusive). | `number()` → `0.190088246732104` |
|
|
50
|
+
| **`random(): number`** | Alias for `float()`. | `random()` → `0.9520779718919631` |
|
|
51
|
+
| **`hex(length: number = 8, prefix: boolean = false): string`** | Generates a random hex string of the specified length (default 8). Optionally prefixes with '0x'. | `hex(16)` → `d1ef0149c7849844` |
|
|
52
|
+
| **`choice(arr: ArrayLike<E>): E`** | Selects a random element from an array. | `choice([1, 2, 3, 4, 5])` → `3` |
|
|
53
|
+
| **`pick(arr: ArrayLike<E>): E`** | Alias for `choice()`. | `pick([1, 2, 3, 4, 5])` → `1` |
|
|
54
|
+
| **`shuffle(input: string): string`**, **`shuffle(input: Array<E>): Array<E>`** | Returns a new array or string after shuffling the given array or string. | `shuffle([1, 2, 3, 4, 5])` → `[ 1, 3, 2, 5, 4 ]` |
|
|
55
|
+
| **`range(min: number, max: number): number`** | Generates a random number (not integer) between given `min` (inclusive) and `max` (exclusive). Throws if `min` > `max`. | `range(1, 5)` → `4.103370176158448` |
|
|
56
|
+
| **`rangeInt(min: number, max: number): number`** | Generates a random number (not integer) between given `min` (inclusive) and `max` (exclusive). Throws if `min` > `max`. | `rangeInt(1, 10)` → `8` |
|
|
57
|
+
| **`randInt(min: number, max: number): number`** | Alias for `rangeInt()`. | `randInt(1, 100)` → `35` |
|
|
58
|
+
| **`safeInt(): number`** | Generates a random integer between `Number.MIN_SAFE_INTEGER` (inclusive) and `Number.MAX_SAFE_INTEGER` (inclusive). +0 and -0 both can be generated. 54 bits precision. Not recommended for genral usage. | `safeInt()` → `-5802548511349229` |
|
|
59
|
+
| **`ifloat(): number`** | Generates a random number between `-1` (inclusive) and `1` (inclusive). Uses `safeInt()` and thus not recommended. | `ifloat()` → `-0.6076475248861822` |
|
|
60
|
+
|
|
61
|
+
© 2025, Md. Touhidur Rahman.
|
|
61
62
|
License: BSD-3-Clause.
|
package/dist/index.d.ts
CHANGED
|
@@ -1,129 +1,34 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
declare module "lib/safeInt" {
|
|
36
|
-
/**
|
|
37
|
-
* Generates a random integer
|
|
38
|
-
* between {@link Number.MIN_SAFE_INTEGER} (inclusive) and {@link Number.MAX_SAFE_INTEGER} (inclusive).
|
|
39
|
-
* +0 and -0 can both appear, and technically 54 bit in precision but not recommended for general usage.
|
|
40
|
-
*
|
|
41
|
-
* Use {@link uSafeInt} for instead.
|
|
42
|
-
**/
|
|
43
|
-
export const safeInt: () => number;
|
|
44
|
-
}
|
|
45
|
-
declare module "lib/ifloat" {
|
|
46
|
-
/**
|
|
47
|
-
* Generates a random number between -1 (inclusive) and 1 (inclusive).
|
|
48
|
-
* Uses {@link safeInt} and thus not recommended for general usage also.
|
|
49
|
-
*
|
|
50
|
-
* Should be somewhat better for calculating PI.
|
|
51
|
-
**/
|
|
52
|
-
export const ifloat: () => number;
|
|
53
|
-
}
|
|
54
|
-
declare module "lib/percentage" {
|
|
55
|
-
/**
|
|
56
|
-
* Generates true with the probability of the percentage value given.
|
|
57
|
-
* @example percentage(20) // should return true 20% of the time and false 80% of the time.
|
|
58
|
-
**/
|
|
59
|
-
export const percentage: (input: number) => boolean;
|
|
60
|
-
}
|
|
61
|
-
declare module "lib/pick" {
|
|
62
|
-
export { choice as pick } from "lib/choice";
|
|
63
|
-
}
|
|
64
|
-
declare module "lib/rangeInt" {
|
|
65
|
-
/**
|
|
66
|
-
* Generates a random number integer
|
|
67
|
-
* between given `min` (inclusive) and `max` (inclusive).
|
|
68
|
-
* Throws if `min` > `max`.
|
|
69
|
-
*
|
|
70
|
-
* @example rangeInt(1, 100) // 35
|
|
71
|
-
*/
|
|
72
|
-
export const rangeInt: (min: number, max: number) => number;
|
|
73
|
-
}
|
|
74
|
-
declare module "lib/randInt" {
|
|
75
|
-
export { rangeInt as randInt } from "lib/rangeInt";
|
|
76
|
-
}
|
|
77
|
-
declare module "lib/random" {
|
|
78
|
-
export { float as random } from "lib/float";
|
|
79
|
-
}
|
|
80
|
-
declare module "lib/range" {
|
|
81
|
-
/**
|
|
82
|
-
* Generates a random number (not integer)
|
|
83
|
-
* between given `min` (inclusive) and `max` (exclusive).
|
|
84
|
-
* Throws if `min` > `max`.
|
|
85
|
-
*
|
|
86
|
-
* @example range(1, 10) // 2.4802525465053487
|
|
87
|
-
*/
|
|
88
|
-
export const range: (min: number, max: number) => number;
|
|
89
|
-
}
|
|
90
|
-
declare module "lib/shuffle" {
|
|
91
|
-
/**
|
|
92
|
-
* Randomly shuffles the elements of an array ***in place***. The original array is modified.
|
|
93
|
-
*/
|
|
94
|
-
export const shuffle: <E>(array: Array<E>) => Array<E>;
|
|
95
|
-
}
|
|
96
|
-
declare module "index" {
|
|
97
|
-
import { bool } from "lib/bool";
|
|
98
|
-
import { boolean } from "lib/boolean";
|
|
99
|
-
import { choice } from "lib/choice";
|
|
100
|
-
import { float } from "lib/float";
|
|
101
|
-
import { ifloat } from "lib/ifloat";
|
|
102
|
-
import { percentage } from "lib/percentage";
|
|
103
|
-
import { pick } from "lib/pick";
|
|
104
|
-
import { randInt } from "lib/randInt";
|
|
105
|
-
import { random } from "lib/random";
|
|
106
|
-
import { range } from "lib/range";
|
|
107
|
-
import { rangeInt } from "lib/rangeInt";
|
|
108
|
-
import { safeInt } from "lib/safeInt";
|
|
109
|
-
import { shuffle } from "lib/shuffle";
|
|
110
|
-
import { uSafeInt } from "lib/uSafeInt";
|
|
111
|
-
export { bool, boolean, choice, float, ifloat, percentage, pick, randInt, random, range, rangeInt, safeInt, shuffle, uSafeInt, };
|
|
112
|
-
const _default: {
|
|
113
|
-
bool: () => boolean;
|
|
114
|
-
boolean: () => boolean;
|
|
115
|
-
choice: <E>(choices: ArrayLike<E>) => E | undefined;
|
|
116
|
-
float: () => number;
|
|
117
|
-
ifloat: () => number;
|
|
118
|
-
percentage: (input: number) => boolean;
|
|
119
|
-
pick: <E>(choices: ArrayLike<E>) => E | undefined;
|
|
120
|
-
randInt: (min: number, max: number) => number;
|
|
121
|
-
random: () => number;
|
|
122
|
-
range: (min: number, max: number) => number;
|
|
123
|
-
rangeInt: (min: number, max: number) => number;
|
|
124
|
-
safeInt: () => number;
|
|
125
|
-
shuffle: <E>(array: Array<E>) => Array<E>;
|
|
126
|
-
uSafeInt: () => number;
|
|
127
|
-
};
|
|
128
|
-
export default _default;
|
|
129
|
-
}
|
|
1
|
+
import { bool } from "./lib/bool";
|
|
2
|
+
import { boolean } from "./lib/boolean";
|
|
3
|
+
import { choice } from "./lib/choice";
|
|
4
|
+
import { float } from "./lib/float";
|
|
5
|
+
import { hex } from "./lib/hex";
|
|
6
|
+
import { ifloat } from "./lib/ifloat";
|
|
7
|
+
import { percentage } from "./lib/percentage";
|
|
8
|
+
import { pick } from "./lib/pick";
|
|
9
|
+
import { randInt } from "./lib/randInt";
|
|
10
|
+
import { random } from "./lib/random";
|
|
11
|
+
import { range } from "./lib/range";
|
|
12
|
+
import { rangeInt } from "./lib/rangeInt";
|
|
13
|
+
import { safeInt } from "./lib/safeInt";
|
|
14
|
+
import { shuffle } from "./lib/shuffle";
|
|
15
|
+
import { uSafeInt } from "./lib/uSafeInt";
|
|
16
|
+
export { bool, boolean, choice, float, hex, ifloat, percentage, pick, randInt, random, range, rangeInt, safeInt, shuffle, uSafeInt, };
|
|
17
|
+
declare const _default: {
|
|
18
|
+
bool: () => boolean;
|
|
19
|
+
boolean: () => boolean;
|
|
20
|
+
choice: <E>(choices: ArrayLike<E>) => E | undefined;
|
|
21
|
+
float: () => number;
|
|
22
|
+
hex: (length?: number, prefix?: boolean) => string;
|
|
23
|
+
ifloat: () => number;
|
|
24
|
+
percentage: (input: number) => boolean;
|
|
25
|
+
pick: <E>(choices: ArrayLike<E>) => E | undefined;
|
|
26
|
+
randInt: (min: number, max: number) => number;
|
|
27
|
+
random: () => number;
|
|
28
|
+
range: (min: number, max: number) => number;
|
|
29
|
+
rangeInt: (min: number, max: number) => number;
|
|
30
|
+
safeInt: () => number;
|
|
31
|
+
shuffle: typeof shuffle;
|
|
32
|
+
uSafeInt: () => number;
|
|
33
|
+
};
|
|
34
|
+
export default _default;
|
package/dist/index.js
CHANGED
|
@@ -20,6 +20,23 @@ var choice = (choices) => {
|
|
|
20
20
|
return choices[Math.floor(float() * choices.length)];
|
|
21
21
|
};
|
|
22
22
|
|
|
23
|
+
// src/lib/hex.ts
|
|
24
|
+
var digits = "0123456789abcdef";
|
|
25
|
+
var hex = (length = 8, prefix = false) => {
|
|
26
|
+
const randArr = crypto.getRandomValues(new Uint8Array(Math.ceil(length / 2)));
|
|
27
|
+
const hexArr = new Array(randArr.length);
|
|
28
|
+
for (let i = 0, j = 0;i < length; i++, j = Math.floor(i / 2)) {
|
|
29
|
+
if (i % 2 === 0) {
|
|
30
|
+
hexArr[i] = digits[randArr[j] >> 4];
|
|
31
|
+
} else {
|
|
32
|
+
hexArr[i] = digits[randArr[j] & 15];
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
if (prefix)
|
|
36
|
+
return "0x" + hexArr.join("");
|
|
37
|
+
return hexArr.join("");
|
|
38
|
+
};
|
|
39
|
+
|
|
23
40
|
// src/lib/safeInt.ts
|
|
24
41
|
var POW_2_372 = Math.pow(2, 37);
|
|
25
42
|
var POW_2_212 = Math.pow(2, 21);
|
|
@@ -55,15 +72,16 @@ var range = (min, max) => {
|
|
|
55
72
|
};
|
|
56
73
|
|
|
57
74
|
// src/lib/shuffle.ts
|
|
58
|
-
|
|
75
|
+
function shuffle(input) {
|
|
76
|
+
const array = Array.from(input);
|
|
59
77
|
for (let i = array.length - 1;i > 0; i--) {
|
|
60
78
|
const pick = Math.floor(float() * (i + 1));
|
|
61
79
|
const temp = array[i];
|
|
62
80
|
array[i] = array[pick];
|
|
63
81
|
array[pick] = temp;
|
|
64
82
|
}
|
|
65
|
-
return array;
|
|
66
|
-
}
|
|
83
|
+
return typeof input === "string" ? array.join("") : array;
|
|
84
|
+
}
|
|
67
85
|
|
|
68
86
|
// src/index.ts
|
|
69
87
|
var src_default = {
|
|
@@ -71,6 +89,7 @@ var src_default = {
|
|
|
71
89
|
boolean: bool,
|
|
72
90
|
choice,
|
|
73
91
|
float,
|
|
92
|
+
hex,
|
|
74
93
|
ifloat,
|
|
75
94
|
percentage,
|
|
76
95
|
pick: choice,
|
|
@@ -93,6 +112,7 @@ export {
|
|
|
93
112
|
choice as pick,
|
|
94
113
|
percentage,
|
|
95
114
|
ifloat,
|
|
115
|
+
hex,
|
|
96
116
|
float,
|
|
97
117
|
src_default as default,
|
|
98
118
|
choice,
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { bool as boolean } from "./bool";
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Randomly choose an element from an array-like object.
|
|
3
|
+
* @returns undefined if the array is empty.
|
|
4
|
+
* @returns a random element from the array if at least one element is present.
|
|
5
|
+
* @example choice([1, 2, 3]) // 2
|
|
6
|
+
*/
|
|
7
|
+
export declare const choice: <E>(choices: ArrayLike<E>) => E | undefined;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Generates a random hex string of the specified length.
|
|
3
|
+
* @param {number} [length=8] - The length of the hex string to generate. Default is `8`.
|
|
4
|
+
* @param {boolean} [prefix=false] - If true, the hex string will be prefixed with "0x". Default `false`.
|
|
5
|
+
* @returns {string} A random hex string of the specified length.
|
|
6
|
+
*/
|
|
7
|
+
export declare const hex: (length?: number, prefix?: boolean) => string;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { choice as pick } from "./choice";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { rangeInt as randInt } from "./rangeInt";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { float as random } from "./float";
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Generates a random integer
|
|
3
|
+
* between {@link Number.MIN_SAFE_INTEGER} (inclusive) and {@link Number.MAX_SAFE_INTEGER} (inclusive).
|
|
4
|
+
* +0 and -0 can both appear, and technically 54 bit in precision but not recommended for general usage.
|
|
5
|
+
*
|
|
6
|
+
* Use {@link uSafeInt} for instead.
|
|
7
|
+
**/
|
|
8
|
+
export declare const safeInt: () => number;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns a new string containing the characters of the input string in a shuffled order.
|
|
3
|
+
*/
|
|
4
|
+
declare function shuffle(input: string): string;
|
|
5
|
+
/**
|
|
6
|
+
* Returns a new array containing the elements of the input array in a shuffled order.
|
|
7
|
+
*/
|
|
8
|
+
declare function shuffle<E>(input: E[]): E[];
|
|
9
|
+
export { shuffle };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "randomcryp",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"license": "BSD-3-Clause",
|
|
5
5
|
"description": "A cryptographically secure, feature rich, zero dependency and browser friendly random number generator library.",
|
|
6
6
|
"author": {
|
|
@@ -11,6 +11,7 @@
|
|
|
11
11
|
"homepage": "https://github.com/touhidurrr/randomcryp",
|
|
12
12
|
"bugs": "https://github.com/touhidurrr/randomcryp/issues",
|
|
13
13
|
"funding": "https://buymeacoffee.com/touhidurrr",
|
|
14
|
+
"type": "module",
|
|
14
15
|
"main": "dist/index.js",
|
|
15
16
|
"module": "dist/index.js",
|
|
16
17
|
"types": "dist/index.d.ts",
|
|
@@ -19,7 +20,7 @@
|
|
|
19
20
|
"build": "bun build src/index.ts --outdir dist && tsc -p tsconfig.dts.json"
|
|
20
21
|
},
|
|
21
22
|
"devDependencies": {
|
|
22
|
-
"@types/bun": "^1.2.
|
|
23
|
+
"@types/bun": "^1.2.8",
|
|
23
24
|
"prettier": "^3.5.3",
|
|
24
25
|
"typescript": "^5.8.2"
|
|
25
26
|
},
|