randomcryp 2.0.0 → 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
@@ -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
@@ -6,6 +6,7 @@ import { hex } from "./lib/hex";
6
6
  import { ifloat } from "./lib/ifloat";
7
7
  import { percentage } from "./lib/percentage";
8
8
  import { pick } from "./lib/pick";
9
+ import { probability } from "./lib/probability";
9
10
  import { randInt } from "./lib/randInt";
10
11
  import { random } from "./lib/random";
11
12
  import { range } from "./lib/range";
@@ -13,7 +14,7 @@ import { rangeInt } from "./lib/rangeInt";
13
14
  import { safeInt } from "./lib/safeInt";
14
15
  import { shuffle } from "./lib/shuffle";
15
16
  import { uSafeInt } from "./lib/uSafeInt";
16
- export { bool, boolean, choice, float, hex, 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, };
17
18
  declare const _default: {
18
19
  bool: () => boolean;
19
20
  boolean: () => boolean;
@@ -21,7 +22,8 @@ declare const _default: {
21
22
  float: () => number;
22
23
  hex: (length?: number, prefix?: boolean) => string;
23
24
  ifloat: () => number;
24
- percentage: (input: number) => boolean;
25
+ percentage: (p: number) => boolean;
26
+ probability: (p: number) => boolean;
25
27
  pick: <E>(choices: ArrayLike<E>) => E | undefined;
26
28
  randInt: (min: number, max: number) => number;
27
29
  random: () => number;
package/dist/index.js CHANGED
@@ -50,12 +50,20 @@ var safeInt = () => {
50
50
  var ifloat = () => safeInt() / Number.MAX_SAFE_INTEGER;
51
51
 
52
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");
53
+ var percentage = (p) => {
54
+ if (p < 0 || p > 100) {
55
+ throw new RangeError(`${p} is not in range [0, 100]`);
56
56
  }
57
- return float() * 100 < input;
57
+ return float() * 100 < p;
58
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
+
59
67
  // src/lib/rangeInt.ts
60
68
  var rangeInt = (min, max) => {
61
69
  if (min > max) {
@@ -92,6 +100,7 @@ var src_default = {
92
100
  hex,
93
101
  ifloat,
94
102
  percentage,
103
+ probability,
95
104
  pick: choice,
96
105
  randInt: rangeInt,
97
106
  random: float,
@@ -109,6 +118,7 @@ export {
109
118
  range,
110
119
  float as random,
111
120
  rangeInt as randInt,
121
+ probability,
112
122
  choice as pick,
113
123
  percentage,
114
124
  ifloat,
@@ -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;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "randomcryp",
3
- "version": "2.0.0",
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": {
@@ -20,9 +20,9 @@
20
20
  "build": "bun build src/index.ts --outdir dist && tsc -p tsconfig.dts.json"
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",