randomcryp 2.0.0 → 2.1.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/README.md CHANGED
@@ -9,7 +9,7 @@ The spelling is random-creep in case you are wondering.
9
9
  - Lightweight
10
10
  - Browser friendly
11
11
  - TypeScript definitions
12
- - Lots of methods and commonn aliases
12
+ - Lots of methods and common aliases
13
13
  - Batteries Included (Zero dependencies)
14
14
  - Supported in all major JavaScript / TypeScript runtimes (Browser, Node, Deno, Bun etc.)
15
15
 
@@ -44,18 +44,19 @@ rangeInt(1, 10); // 7
44
44
  | -- | -- | -- |
45
45
  | **`bool(): boolean`** | Generates a random boolean (`true` or `false`). | `bool()` → `true` |
46
46
  | **`boolean(): boolean`** | Alias for `bool()`. | `boolean()` → `false` |
47
- | **`percentage(val: number): boolean`** | Generates `true` at given percentage of time. | `percentage(20)` → `false` |
47
+ | **`percentage(p: number): boolean`** | Generates `true` at given percentage of time. | `percentage(20)` → `false` |
48
+ | **`probability(p: number): boolean`** | Generates `true` with a given probability `p` and `false` with probability `1-p`. | `probability(0.8)` → `true` |
48
49
  | **`uSafeInt(): number`** | Generates a random integer between `0` (inclusive) and `Number.MAX_SAFE_INTEGER` (inclusive). | `uSafeInt()` → `4946544243668033` |
49
50
  | **`float(): number`** | Generates a random number between `0` (inclusive) and `1` (exclusive). | `number()` → `0.190088246732104` |
50
51
  | **`random(): number`** | Alias for `float()`. | `random()` → `0.9520779718919631` |
51
52
  | **`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
53
  | **`choice(arr: ArrayLike<E>): E`** | Selects a random element from an array. | `choice([1, 2, 3, 4, 5])` → `3` |
53
54
  | **`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
+ | **`shuffle(input: string): string`**, **`shuffle(input: Array<E>): Array<E>`** | Returns a new array or string after shuffling the given array or string randomly. | `shuffle([1, 2, 3, 4, 5])` → `[ 1, 3, 2, 5, 4 ]` |
55
56
  | **`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
57
  | **`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
58
  | **`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
+ | **`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 general usage. | `safeInt()` → `-5802548511349229` |
59
60
  | **`ifloat(): number`** | Generates a random number between `-1` (inclusive) and `1` (inclusive). Uses `safeInt()` and thus not recommended. | `ifloat()` → `-0.6076475248861822` |
60
61
 
61
62
  © 2025, Md. Touhidur Rahman.
package/dist/index.d.ts CHANGED
@@ -1,34 +1,54 @@
1
- import { bool } from "./lib/bool";
2
- import { boolean } from "./lib/boolean";
1
+ import { ifloat } from "./lib/ifloat";
3
2
  import { choice } from "./lib/choice";
3
+ import { randInt } from "./lib/randInt";
4
4
  import { float } from "./lib/float";
5
- import { hex } from "./lib/hex";
6
- import { ifloat } from "./lib/ifloat";
5
+ import { bool } from "./lib/bool";
7
6
  import { percentage } from "./lib/percentage";
8
- import { pick } from "./lib/pick";
9
- import { randInt } from "./lib/randInt";
10
- import { random } from "./lib/random";
7
+ import { uSafeInt } from "./lib/uSafeInt";
8
+ import { probability } from "./lib/probability";
11
9
  import { range } from "./lib/range";
10
+ import { random } from "./lib/random";
12
11
  import { rangeInt } from "./lib/rangeInt";
12
+ import { boolean } from "./lib/boolean";
13
+ import { hex } from "./lib/hex";
13
14
  import { safeInt } from "./lib/safeInt";
15
+ import { pick } from "./lib/pick";
14
16
  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;
17
+
18
+ export {
19
+ ifloat,
20
+ choice,
21
+ randInt,
22
+ float,
23
+ bool,
24
+ percentage,
25
+ uSafeInt,
26
+ probability,
27
+ range,
28
+ random,
29
+ rangeInt,
30
+ boolean,
31
+ hex,
32
+ safeInt,
33
+ pick,
34
+ shuffle,
35
+ };
36
+
37
+ export default {
38
+ ifloat,
39
+ choice,
40
+ randInt,
41
+ float,
42
+ bool,
43
+ percentage,
44
+ uSafeInt,
45
+ probability,
46
+ range,
47
+ random,
48
+ rangeInt,
49
+ boolean,
50
+ hex,
51
+ safeInt,
52
+ pick,
53
+ shuffle,
33
54
  };
34
- export default _default;
package/dist/index.js CHANGED
@@ -1,121 +1 @@
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/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
-
40
- // src/lib/safeInt.ts
41
- var POW_2_372 = Math.pow(2, 37);
42
- var POW_2_212 = Math.pow(2, 21);
43
- var POW_2_52 = Math.pow(2, 5);
44
- var safeInt = () => {
45
- const [num1, num2, num3, num4] = crypto.getRandomValues(new Uint16Array(4));
46
- 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));
47
- };
48
-
49
- // src/lib/ifloat.ts
50
- var ifloat = () => safeInt() / Number.MAX_SAFE_INTEGER;
51
-
52
- // src/lib/percentage.ts
53
- var percentage = (input) => {
54
- if (input < 0 || input > 100) {
55
- throw new RangeError("input value should be between 0 and 100");
56
- }
57
- return float() * 100 < input;
58
- };
59
- // src/lib/rangeInt.ts
60
- var rangeInt = (min, max) => {
61
- if (min > max) {
62
- throw new RangeError("min must be less than or equal to max");
63
- }
64
- return min + Math.floor(float() * (max - min + 1));
65
- };
66
- // src/lib/range.ts
67
- var range = (min, max) => {
68
- if (min > max) {
69
- throw new RangeError("min must be less than or equal to max");
70
- }
71
- return min + float() * (max - min);
72
- };
73
-
74
- // src/lib/shuffle.ts
75
- function shuffle(input) {
76
- const array = Array.from(input);
77
- for (let i = array.length - 1;i > 0; i--) {
78
- const pick = Math.floor(float() * (i + 1));
79
- const temp = array[i];
80
- array[i] = array[pick];
81
- array[pick] = temp;
82
- }
83
- return typeof input === "string" ? array.join("") : array;
84
- }
85
-
86
- // src/index.ts
87
- var src_default = {
88
- bool,
89
- boolean: bool,
90
- choice,
91
- float,
92
- hex,
93
- ifloat,
94
- percentage,
95
- pick: choice,
96
- randInt: rangeInt,
97
- random: float,
98
- range,
99
- rangeInt,
100
- safeInt,
101
- shuffle,
102
- uSafeInt
103
- };
104
- export {
105
- uSafeInt,
106
- shuffle,
107
- safeInt,
108
- rangeInt,
109
- range,
110
- float as random,
111
- rangeInt as randInt,
112
- choice as pick,
113
- percentage,
114
- ifloat,
115
- hex,
116
- float,
117
- src_default as default,
118
- choice,
119
- bool as boolean,
120
- bool
121
- };
1
+ var k=Math.pow(2,37),N=Math.pow(2,21),D=Math.pow(2,5),L=()=>{let[z,B,H,J]=crypto.getRandomValues(new Uint16Array(4));return z*k+B*N+H*D+(J&D-1)};var Q=()=>L()%2===0;var R=Math.pow(2,53),C=()=>L()/R;var V=(z)=>{if(z.length===0)return;return z[Math.floor(C()*z.length)]};var U=(z=8,B=!1)=>{let H=crypto.getRandomValues(new Uint8Array(Math.ceil(z/2))),J=new Array(H.length);for(let K=0,$=0;K<z;K++,$=Math.floor(K/2))if(K%2===0)J[K]="0123456789abcdef"[H[$]>>4];else J[K]="0123456789abcdef"[H[$]&15];if(B)return"0x"+J.join("");return J.join("")};var S=Math.pow(2,37),I=Math.pow(2,21),q=Math.pow(2,5),Z=()=>{let[z,B,H,J]=crypto.getRandomValues(new Uint16Array(4));return((J&q)<1?1:-1)*(z*S+B*I+H*q+(J&q-1))};var X=()=>Z()/Number.MAX_SAFE_INTEGER;var F=(z)=>{if(z<0||z>100)throw new RangeError(`${z} is not in range [0, 100]`);return C()*100<z};var M=(z)=>{if(z<0||z>1)throw new RangeError(`${z} is not in range [0, 1]`);return C()<z};var Y=(z,B)=>{if(z>B)throw new RangeError("min must be less than or equal to max");return z+Math.floor(C()*(B-z+1))};var j=(z,B)=>{if(z>B)throw new RangeError("min must be less than or equal to max");return z+C()*(B-z)};function T(z){let B=Array.from(z);for(let H=B.length-1;H>0;H--){let J=Math.floor(C()*(H+1)),K=B[H];B[H]=B[J],B[J]=K}return typeof z==="string"?B.join(""):B}var Fz={bool:Q,boolean:Q,choice:V,float:C,hex:U,ifloat:X,percentage:F,probability:M,pick:V,randInt:Y,random:C,range:j,rangeInt:Y,safeInt:Z,shuffle:T,uSafeInt:L};export{L as uSafeInt,T as shuffle,Z as safeInt,Y as rangeInt,j as range,C as random,Y as randInt,M as probability,V as pick,F as percentage,X as ifloat,U as hex,C as float,Fz as default,V as choice,Q as boolean,Q as bool};
@@ -1,5 +1,5 @@
1
1
  /**
2
- * Generates a boolean value randomly.
3
- * true and false are equally likely.
2
+ * Generates a `boolean` value randomly.
3
+ * `true` and `false` are equally likely.
4
4
  **/
5
5
  export declare const bool: () => boolean;
@@ -1,6 +1,6 @@
1
1
  /**
2
2
  * Randomly choose an element from an array-like object.
3
- * @returns undefined if the array is empty.
3
+ * @returns `undefined` if the array is empty.
4
4
  * @returns a random element from the array if at least one element is present.
5
5
  * @example choice([1, 2, 3]) // 2
6
6
  */
@@ -1,5 +1,5 @@
1
1
  /**
2
- * Generates a random number between 0 (inclusive) and 1 (exclusive).
3
- * with 53 bits precission.
2
+ * Generates a random number between `0` (inclusive) and `1` (exclusive).
3
+ * with `53` bits precission.
4
4
  **/
5
5
  export declare const float: () => number;
package/dist/lib/hex.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * Generates a random hex string of the specified length.
3
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`.
4
+ * @param {boolean} [prefix=false] - If `true`, the hex string will be prefixed with `"0x"`. Default `false`.
5
5
  * @returns {string} A random hex string of the specified length.
6
6
  */
7
7
  export declare const hex: (length?: number, prefix?: boolean) => string;
@@ -1,5 +1,5 @@
1
1
  /**
2
- * Generates a random number between -1 (inclusive) and 1 (inclusive).
2
+ * Generates a random number between `-1` (inclusive) and `1` (inclusive).
3
3
  * Uses {@link safeInt} and thus not recommended for general usage also.
4
4
  *
5
5
  * Should be somewhat better for calculating PI.
@@ -1,5 +1,6 @@
1
1
  /**
2
- * Generates true with the probability of the percentage value given.
2
+ * Generates `true` with the probability of the percentage value given.
3
3
  * @example percentage(20) // should return true 20% of the time and false 80% of the time.
4
+ * @throws RangeError if `p` is not range `[0, 100]`.
4
5
  **/
5
- export declare const percentage: (input: number) => boolean;
6
+ export declare const percentage: (p: number) => boolean;
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Generates `true` with a given probability `p` and `false` with probability `1-p`.
3
+ * @throws {RangeError} If `p` is not in range `[0, 1]`.
4
+ * @example probability(0.8); // returns true 80% of the time and false 20% of the time.
5
+ */
6
+ export declare const probability: (p: number) => boolean;
@@ -1,6 +1,6 @@
1
1
  /**
2
2
  * Generates a random integer
3
- * between 0 (inclusive) and {@link Number.MAX_SAFE_INTEGER} (inclusive)
4
- * with 53 bits precission.
3
+ * between `0` (inclusive) and {@link Number.MAX_SAFE_INTEGER} (inclusive)
4
+ * with `53` bits precission.
5
5
  **/
6
6
  export declare const uSafeInt: () => number;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "randomcryp",
3
- "version": "2.0.0",
3
+ "version": "2.1.1",
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": {
@@ -17,12 +17,12 @@
17
17
  "types": "dist/index.d.ts",
18
18
  "scripts": {
19
19
  "format": "prettier --write .",
20
- "build": "bun build src/index.ts --outdir dist && tsc -p tsconfig.dts.json"
20
+ "build": "bun scripts/build.ts"
21
21
  },
22
22
  "devDependencies": {
23
- "@types/bun": "^1.2.8",
23
+ "@types/bun": "^1.2.9",
24
24
  "prettier": "^3.5.3",
25
- "typescript": "^5.8.2"
25
+ "typescript": "^5.8.3"
26
26
  },
27
27
  "keywords": [
28
28
  "cryptographically-secure",