randomcryp 1.0.4
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/LICENSE +28 -0
- package/README.md +60 -0
- package/dist/index.d.ts +129 -0
- package/dist/index.js +101 -0
- package/package.json +25 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
BSD 3-Clause License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025, Md. Touhidur Rahman
|
|
4
|
+
|
|
5
|
+
Redistribution and use in source and binary forms, with or without
|
|
6
|
+
modification, are permitted provided that the following conditions are met:
|
|
7
|
+
|
|
8
|
+
1. Redistributions of source code must retain the above copyright notice, this
|
|
9
|
+
list of conditions and the following disclaimer.
|
|
10
|
+
|
|
11
|
+
2. Redistributions in binary form must reproduce the above copyright notice,
|
|
12
|
+
this list of conditions and the following disclaimer in the documentation
|
|
13
|
+
and/or other materials provided with the distribution.
|
|
14
|
+
|
|
15
|
+
3. Neither the name of the copyright holder nor the names of its
|
|
16
|
+
contributors may be used to endorse or promote products derived from
|
|
17
|
+
this software without specific prior written permission.
|
|
18
|
+
|
|
19
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
20
|
+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
21
|
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
22
|
+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
23
|
+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
24
|
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
25
|
+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
26
|
+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
27
|
+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
28
|
+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
package/README.md
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
# randomcryp
|
|
2
|
+
|
|
3
|
+
A cryptographically secure, feature rich, zero dependency, lightweight and browser friendly random number generator library. Only ~3KB in size.
|
|
4
|
+
Uses [Crypto.getRandomValues()](https://developer.mozilla.org/en-US/docs/Web/API/Crypto/getRandomValues) to get its randomness.
|
|
5
|
+
The spelling is random-creep in case you are wondering.
|
|
6
|
+
|
|
7
|
+
## Featues
|
|
8
|
+
|
|
9
|
+
- Lightweight
|
|
10
|
+
- Browser friendly
|
|
11
|
+
- TypeScript definitions
|
|
12
|
+
- Lots of methods and commonn aliases
|
|
13
|
+
- Batteries Included (Zero dependencies)
|
|
14
|
+
- Supported in all major JavaScript / TypeScript runtimes (Browser, Node, Deno, Bun etc.)
|
|
15
|
+
|
|
16
|
+
## Install
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
bun add randomcryp # bun
|
|
20
|
+
yarn add randomcryp # yarn
|
|
21
|
+
pnpm add randomcryp # pnpm
|
|
22
|
+
npm install randomcryp # npm
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## Usage
|
|
26
|
+
|
|
27
|
+
```ts
|
|
28
|
+
import random from "randomcryp";
|
|
29
|
+
|
|
30
|
+
float(); // 0.190088246732104
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
Or,
|
|
34
|
+
|
|
35
|
+
```ts
|
|
36
|
+
import { rangeInt } from "randomcryp";
|
|
37
|
+
|
|
38
|
+
rangeInt(1, 10); // 7
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
#### List of Methods
|
|
42
|
+
|
|
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
|
+
| **`choice(arr: ArrayLike<E>): E`** | Selects a random element from an array. | `choice([1, 2, 3, 4, 5])` → `3` |
|
|
52
|
+
| **`pick(arr: ArrayLike<E>): E`** | Alias for `choice()`. | `pick([1, 2, 3, 4, 5])` → `1` |
|
|
53
|
+
| **`shuffle(arr: Array<E>): E`** | Shuffles the elements of an array randomly in-place and returns a reference to the original array. | `shuffle([1, 2, 3, 4, 5])` → `[ 1, 3, 2, 5, 4 ]` |
|
|
54
|
+
| **`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` |
|
|
55
|
+
| **`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` |
|
|
56
|
+
| **`randInt(min: number, max: number): number`** | Alias for `rangeInt()`. | `randInt(1, 100)` → `35` |
|
|
57
|
+
| **`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` |
|
|
58
|
+
| **`ifloat(): number`** | Generates a random number between `-1` (inclusive) and `1` (inclusive). Uses `safeInt()` and thus not recommended. | `ifloat()` → `-0.6076475248861822` |
|
|
59
|
+
|
|
60
|
+
License, BSD-3-Clause
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
declare module "lib/uSafeInt" {
|
|
2
|
+
/**
|
|
3
|
+
* Generates a random integer
|
|
4
|
+
* between 0 (inclusive) and {@link Number.MAX_SAFE_INTEGER} (inclusive)
|
|
5
|
+
* with 53 bits precission.
|
|
6
|
+
**/
|
|
7
|
+
export const uSafeInt: () => number;
|
|
8
|
+
}
|
|
9
|
+
declare module "lib/bool" {
|
|
10
|
+
/**
|
|
11
|
+
* Generates a boolean value randomly.
|
|
12
|
+
* true and false are equally likely.
|
|
13
|
+
**/
|
|
14
|
+
export const bool: () => boolean;
|
|
15
|
+
}
|
|
16
|
+
declare module "lib/boolean" {
|
|
17
|
+
export { bool as boolean } from "lib/bool";
|
|
18
|
+
}
|
|
19
|
+
declare module "lib/float" {
|
|
20
|
+
/**
|
|
21
|
+
* Generates a random number between 0 (inclusive) and 1 (exclusive).
|
|
22
|
+
* with 53 bits precission.
|
|
23
|
+
**/
|
|
24
|
+
export const float: () => number;
|
|
25
|
+
}
|
|
26
|
+
declare module "lib/choice" {
|
|
27
|
+
/**
|
|
28
|
+
* Randomly choose an element from an array-like object.
|
|
29
|
+
* @returns undefined if the array is empty.
|
|
30
|
+
* @returns a random element from the array if at least one element is present.
|
|
31
|
+
* @example choice([1, 2, 3]) // 2
|
|
32
|
+
*/
|
|
33
|
+
export const choice: <E>(choices: ArrayLike<E>) => E | undefined;
|
|
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
|
+
}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
// src/lib/uSafeInt.ts
|
|
2
|
+
var POW_2_37 = Math.pow(2, 37);
|
|
3
|
+
var POW_2_21 = Math.pow(2, 21);
|
|
4
|
+
var POW_2_5 = Math.pow(2, 5);
|
|
5
|
+
var uSafeInt = () => {
|
|
6
|
+
const [num1, num2, num3, num4] = crypto.getRandomValues(new Uint16Array(4));
|
|
7
|
+
return num1 * POW_2_37 + num2 * POW_2_21 + num3 * POW_2_5 + (num4 & POW_2_5 - 1);
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
// src/lib/bool.ts
|
|
11
|
+
var bool = () => uSafeInt() % 2 === 0;
|
|
12
|
+
// src/lib/float.ts
|
|
13
|
+
var POW_2_53 = Math.pow(2, 53);
|
|
14
|
+
var float = () => uSafeInt() / POW_2_53;
|
|
15
|
+
|
|
16
|
+
// src/lib/choice.ts
|
|
17
|
+
var choice = (choices) => {
|
|
18
|
+
if (choices.length === 0)
|
|
19
|
+
return;
|
|
20
|
+
return choices[Math.floor(float() * choices.length)];
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
// src/lib/safeInt.ts
|
|
24
|
+
var POW_2_372 = Math.pow(2, 37);
|
|
25
|
+
var POW_2_212 = Math.pow(2, 21);
|
|
26
|
+
var POW_2_52 = Math.pow(2, 5);
|
|
27
|
+
var safeInt = () => {
|
|
28
|
+
const [num1, num2, num3, num4] = crypto.getRandomValues(new Uint16Array(4));
|
|
29
|
+
return ((num4 & POW_2_52) < 1 ? 1 : -1) * (num1 * POW_2_372 + num2 * POW_2_212 + num3 * POW_2_52 + (num4 & POW_2_52 - 1));
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
// src/lib/ifloat.ts
|
|
33
|
+
var ifloat = () => safeInt() / Number.MAX_SAFE_INTEGER;
|
|
34
|
+
|
|
35
|
+
// src/lib/percentage.ts
|
|
36
|
+
var percentage = (input) => {
|
|
37
|
+
if (input < 0 || input > 100) {
|
|
38
|
+
throw new RangeError("input value should be between 0 and 100");
|
|
39
|
+
}
|
|
40
|
+
return float() * 100 < input;
|
|
41
|
+
};
|
|
42
|
+
// src/lib/rangeInt.ts
|
|
43
|
+
var rangeInt = (min, max) => {
|
|
44
|
+
if (min > max) {
|
|
45
|
+
throw new RangeError("min must be less than or equal to max");
|
|
46
|
+
}
|
|
47
|
+
return min + Math.floor(float() * (max - min + 1));
|
|
48
|
+
};
|
|
49
|
+
// src/lib/range.ts
|
|
50
|
+
var range = (min, max) => {
|
|
51
|
+
if (min > max) {
|
|
52
|
+
throw new RangeError("min must be less than or equal to max");
|
|
53
|
+
}
|
|
54
|
+
return min + float() * (max - min);
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
// src/lib/shuffle.ts
|
|
58
|
+
var shuffle = (array) => {
|
|
59
|
+
for (let i = array.length - 1;i > 0; i--) {
|
|
60
|
+
const pick = Math.floor(float() * (i + 1));
|
|
61
|
+
const temp = array[i];
|
|
62
|
+
array[i] = array[pick];
|
|
63
|
+
array[pick] = temp;
|
|
64
|
+
}
|
|
65
|
+
return array;
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
// src/index.ts
|
|
69
|
+
var src_default = {
|
|
70
|
+
bool,
|
|
71
|
+
boolean: bool,
|
|
72
|
+
choice,
|
|
73
|
+
float,
|
|
74
|
+
ifloat,
|
|
75
|
+
percentage,
|
|
76
|
+
pick: choice,
|
|
77
|
+
randInt: rangeInt,
|
|
78
|
+
random: float,
|
|
79
|
+
range,
|
|
80
|
+
rangeInt,
|
|
81
|
+
safeInt,
|
|
82
|
+
shuffle,
|
|
83
|
+
uSafeInt
|
|
84
|
+
};
|
|
85
|
+
export {
|
|
86
|
+
uSafeInt,
|
|
87
|
+
shuffle,
|
|
88
|
+
safeInt,
|
|
89
|
+
rangeInt,
|
|
90
|
+
range,
|
|
91
|
+
float as random,
|
|
92
|
+
rangeInt as randInt,
|
|
93
|
+
choice as pick,
|
|
94
|
+
percentage,
|
|
95
|
+
ifloat,
|
|
96
|
+
float,
|
|
97
|
+
src_default as default,
|
|
98
|
+
choice,
|
|
99
|
+
bool as boolean,
|
|
100
|
+
bool
|
|
101
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "randomcryp",
|
|
3
|
+
"version": "1.0.4",
|
|
4
|
+
"license": "BSD-3-Clause",
|
|
5
|
+
"description": "A cryptographically secure, feature rich, zero dependency and browser friendly random number generator library.",
|
|
6
|
+
"author": {
|
|
7
|
+
"name": "Md. Touhidur Rahman",
|
|
8
|
+
"email": "touhidurrr@pm.me"
|
|
9
|
+
},
|
|
10
|
+
"repository": "github:touhidurrr/randomcryp",
|
|
11
|
+
"homepage": "https://github.com/touhidurrr/randomcryp",
|
|
12
|
+
"bugs": "https://github.com/touhidurrr/randomcryp/issues",
|
|
13
|
+
"funding": "https://buymeacoffee.com/touhidurrr",
|
|
14
|
+
"main": "dist/index.js",
|
|
15
|
+
"types": "dist/index.d.ts",
|
|
16
|
+
"scripts": {
|
|
17
|
+
"format": "prettier --write .",
|
|
18
|
+
"build": "bun build src/index.ts --outdir dist && tsc -p tsconfig.dts.json"
|
|
19
|
+
},
|
|
20
|
+
"devDependencies": {
|
|
21
|
+
"@types/bun": "^1.2.5",
|
|
22
|
+
"prettier": "^3.5.3",
|
|
23
|
+
"typescript": "^5.8.2"
|
|
24
|
+
}
|
|
25
|
+
}
|