randomcryp 1.0.6 → 2.1.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 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 ~4KB in size.
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
 
@@ -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
 
@@ -40,22 +40,24 @@ rangeInt(1, 10); // 7
40
40
 
41
41
  #### List of Methods
42
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
- © 2025, Md. Touhidur Rahman
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(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` |
49
+ | **`uSafeInt(): number`** | Generates a random integer between `0` (inclusive) and `Number.MAX_SAFE_INTEGER` (inclusive). | `uSafeInt()` → `4946544243668033` |
50
+ | **`float(): number`** | Generates a random number between `0` (inclusive) and `1` (exclusive). | `number()` → `0.190088246732104` |
51
+ | **`random(): number`** | Alias for `float()`. | `random()` → `0.9520779718919631` |
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` |
53
+ | **`choice(arr: ArrayLike<E>): E`** | Selects a random element from an array. | `choice([1, 2, 3, 4, 5])` → `3` |
54
+ | **`pick(arr: ArrayLike<E>): E`** | Alias for `choice()`. | `pick([1, 2, 3, 4, 5])` → `1` |
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 ]` |
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` |
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` |
58
+ | **`randInt(min: number, max: number): number`** | Alias for `rangeInt()`. | `randInt(1, 100)` → `35` |
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` |
60
+ | **`ifloat(): number`** | Generates a random number between `-1` (inclusive) and `1` (inclusive). Uses `safeInt()` and thus not recommended. | `ifloat()` → `-0.6076475248861822` |
61
+
62
+ © 2025, Md. Touhidur Rahman.
61
63
  License: BSD-3-Clause.
package/dist/index.d.ts CHANGED
@@ -2,9 +2,11 @@ import { bool } from "./lib/bool";
2
2
  import { boolean } from "./lib/boolean";
3
3
  import { choice } from "./lib/choice";
4
4
  import { float } from "./lib/float";
5
+ import { hex } from "./lib/hex";
5
6
  import { ifloat } from "./lib/ifloat";
6
7
  import { percentage } from "./lib/percentage";
7
8
  import { pick } from "./lib/pick";
9
+ import { probability } from "./lib/probability";
8
10
  import { randInt } from "./lib/randInt";
9
11
  import { random } from "./lib/random";
10
12
  import { range } from "./lib/range";
@@ -12,21 +14,23 @@ import { rangeInt } from "./lib/rangeInt";
12
14
  import { safeInt } from "./lib/safeInt";
13
15
  import { shuffle } from "./lib/shuffle";
14
16
  import { uSafeInt } from "./lib/uSafeInt";
15
- export { bool, boolean, choice, float, ifloat, percentage, pick, randInt, random, range, rangeInt, safeInt, shuffle, uSafeInt, };
17
+ export { bool, boolean, choice, float, hex, ifloat, percentage, pick, probability, randInt, random, range, rangeInt, safeInt, shuffle, uSafeInt, };
16
18
  declare const _default: {
17
19
  bool: () => boolean;
18
20
  boolean: () => boolean;
19
21
  choice: <E>(choices: ArrayLike<E>) => E | undefined;
20
22
  float: () => number;
23
+ hex: (length?: number, prefix?: boolean) => string;
21
24
  ifloat: () => number;
22
- percentage: (input: number) => boolean;
25
+ percentage: (p: number) => boolean;
26
+ probability: (p: number) => boolean;
23
27
  pick: <E>(choices: ArrayLike<E>) => E | undefined;
24
28
  randInt: (min: number, max: number) => number;
25
29
  random: () => number;
26
30
  range: (min: number, max: number) => number;
27
31
  rangeInt: (min: number, max: number) => number;
28
32
  safeInt: () => number;
29
- shuffle: <E>(array: Array<E>) => Array<E>;
33
+ shuffle: typeof shuffle;
30
34
  uSafeInt: () => number;
31
35
  };
32
36
  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);
@@ -33,12 +50,20 @@ var safeInt = () => {
33
50
  var ifloat = () => safeInt() / Number.MAX_SAFE_INTEGER;
34
51
 
35
52
  // 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");
53
+ var percentage = (p) => {
54
+ if (p < 0 || p > 100) {
55
+ throw new RangeError(`${p} is not in range [0, 100]`);
39
56
  }
40
- return float() * 100 < input;
57
+ return float() * 100 < p;
41
58
  };
59
+ // src/lib/probability.ts
60
+ var probability = (p) => {
61
+ if (p < 0 || p > 1) {
62
+ throw new RangeError(`${p} is not in range [0, 1]`);
63
+ }
64
+ return float() < p;
65
+ };
66
+
42
67
  // src/lib/rangeInt.ts
43
68
  var rangeInt = (min, max) => {
44
69
  if (min > max) {
@@ -55,15 +80,16 @@ var range = (min, max) => {
55
80
  };
56
81
 
57
82
  // src/lib/shuffle.ts
58
- var shuffle = (array) => {
83
+ function shuffle(input) {
84
+ const array = Array.from(input);
59
85
  for (let i = array.length - 1;i > 0; i--) {
60
86
  const pick = Math.floor(float() * (i + 1));
61
87
  const temp = array[i];
62
88
  array[i] = array[pick];
63
89
  array[pick] = temp;
64
90
  }
65
- return array;
66
- };
91
+ return typeof input === "string" ? array.join("") : array;
92
+ }
67
93
 
68
94
  // src/index.ts
69
95
  var src_default = {
@@ -71,8 +97,10 @@ var src_default = {
71
97
  boolean: bool,
72
98
  choice,
73
99
  float,
100
+ hex,
74
101
  ifloat,
75
102
  percentage,
103
+ probability,
76
104
  pick: choice,
77
105
  randInt: rangeInt,
78
106
  random: float,
@@ -90,9 +118,11 @@ export {
90
118
  range,
91
119
  float as random,
92
120
  rangeInt as randInt,
121
+ probability,
93
122
  choice as pick,
94
123
  percentage,
95
124
  ifloat,
125
+ hex,
96
126
  float,
97
127
  src_default as default,
98
128
  choice,
@@ -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;
@@ -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,4 +1,9 @@
1
1
  /**
2
- * Randomly shuffles the elements of an array ***in place***. The original array is modified.
2
+ * Returns a new string containing the characters of the input string in a shuffled order.
3
3
  */
4
- export declare const shuffle: <E>(array: Array<E>) => Array<E>;
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": "1.0.6",
3
+ "version": "2.1.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,9 +20,9 @@
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.5",
23
+ "@types/bun": "^1.2.9",
23
24
  "prettier": "^3.5.3",
24
- "typescript": "^5.8.2"
25
+ "typescript": "^5.8.3"
25
26
  },
26
27
  "keywords": [
27
28
  "cryptographically-secure",