randomcryp 2.1.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
@@ -56,7 +56,7 @@ rangeInt(1, 10); // 7
56
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
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
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` |
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
60
  | **`ifloat(): number`** | Generates a random number between `-1` (inclusive) and `1` (inclusive). Uses `safeInt()` and thus not recommended. | `ifloat()` → `-0.6076475248861822` |
61
61
 
62
62
  © 2025, Md. Touhidur Rahman.
package/dist/index.d.ts CHANGED
@@ -1,36 +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";
7
+ import { uSafeInt } from "./lib/uSafeInt";
9
8
  import { probability } from "./lib/probability";
10
- import { randInt } from "./lib/randInt";
11
- import { random } from "./lib/random";
12
9
  import { range } from "./lib/range";
10
+ import { random } from "./lib/random";
13
11
  import { rangeInt } from "./lib/rangeInt";
12
+ import { boolean } from "./lib/boolean";
13
+ import { hex } from "./lib/hex";
14
14
  import { safeInt } from "./lib/safeInt";
15
+ import { pick } from "./lib/pick";
15
16
  import { shuffle } from "./lib/shuffle";
16
- import { uSafeInt } from "./lib/uSafeInt";
17
- export { bool, boolean, choice, float, hex, ifloat, percentage, pick, probability, randInt, random, range, rangeInt, safeInt, shuffle, uSafeInt, };
18
- declare const _default: {
19
- bool: () => boolean;
20
- boolean: () => boolean;
21
- choice: <E>(choices: ArrayLike<E>) => E | undefined;
22
- float: () => number;
23
- hex: (length?: number, prefix?: boolean) => string;
24
- ifloat: () => number;
25
- percentage: (p: number) => boolean;
26
- probability: (p: number) => boolean;
27
- pick: <E>(choices: ArrayLike<E>) => E | undefined;
28
- randInt: (min: number, max: number) => number;
29
- random: () => number;
30
- range: (min: number, max: number) => number;
31
- rangeInt: (min: number, max: number) => number;
32
- safeInt: () => number;
33
- shuffle: typeof shuffle;
34
- 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,
35
54
  };
36
- export default _default;
package/dist/index.js CHANGED
@@ -1,131 +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 = (p) => {
54
- if (p < 0 || p > 100) {
55
- throw new RangeError(`${p} is not in range [0, 100]`);
56
- }
57
- return float() * 100 < p;
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
-
67
- // src/lib/rangeInt.ts
68
- var rangeInt = (min, max) => {
69
- if (min > max) {
70
- throw new RangeError("min must be less than or equal to max");
71
- }
72
- return min + Math.floor(float() * (max - min + 1));
73
- };
74
- // src/lib/range.ts
75
- var range = (min, max) => {
76
- if (min > max) {
77
- throw new RangeError("min must be less than or equal to max");
78
- }
79
- return min + float() * (max - min);
80
- };
81
-
82
- // src/lib/shuffle.ts
83
- function shuffle(input) {
84
- const array = Array.from(input);
85
- for (let i = array.length - 1;i > 0; i--) {
86
- const pick = Math.floor(float() * (i + 1));
87
- const temp = array[i];
88
- array[i] = array[pick];
89
- array[pick] = temp;
90
- }
91
- return typeof input === "string" ? array.join("") : array;
92
- }
93
-
94
- // src/index.ts
95
- var src_default = {
96
- bool,
97
- boolean: bool,
98
- choice,
99
- float,
100
- hex,
101
- ifloat,
102
- percentage,
103
- probability,
104
- pick: choice,
105
- randInt: rangeInt,
106
- random: float,
107
- range,
108
- rangeInt,
109
- safeInt,
110
- shuffle,
111
- uSafeInt
112
- };
113
- export {
114
- uSafeInt,
115
- shuffle,
116
- safeInt,
117
- rangeInt,
118
- range,
119
- float as random,
120
- rangeInt as randInt,
121
- probability,
122
- choice as pick,
123
- percentage,
124
- ifloat,
125
- hex,
126
- float,
127
- src_default as default,
128
- choice,
129
- bool as boolean,
130
- bool
131
- };
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,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.1.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,7 +17,7 @@
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
23
  "@types/bun": "^1.2.9",