@xtia/alea-rc 0.0.10 → 0.0.12

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
@@ -6,11 +6,10 @@ Alea is a utility wrapper for turning random numbers into useful values. Give it
6
6
 
7
7
  * Fully typed
8
8
  * Crypto-safe and seeded algorithms out-of-the-box
9
+ * Expressive: code with intent
9
10
  * No dependencies
10
- * ~2.6kb minified core
11
- * Ranged int, array shuffling, dice roll, weighted sampling, recursive template phrase generation, UUID, bytes and many more
12
-
13
- ## Brief:
11
+ * ~2.4kb minified core
12
+ * Array shuffling, weighted sampling, recursive template phrase generation, UUID, bytes and many more
14
13
 
15
14
  `npm i @xtia/alea` (pending release; use `@xtia/alea-rc` to preview)
16
15
 
@@ -18,14 +17,13 @@ Alea is a utility wrapper for turning random numbers into useful values. Give it
18
17
  import { alea, cryptoAlea } from "@xtia/alea";
19
18
 
20
19
  // generate values (driven by Math.random())
21
- const damage = alea.roll(2, 6); // 2d6
22
- const duration = alea.between(1000, 1500);
20
+ const damage = alea.between(50, 150);
23
21
  const loot = alea.chance(0.125) ? "epic" : "common";
24
22
  const id = alea.string(5, "abcdef0123456789");
25
23
  const npcName = alea.sample(["Alice", "Bob", "Charlie"]);
26
24
 
27
- // secure source (driven by environment's crypto)
28
- const key = cryptoAlea.string(16);
25
+ // same API, secure source (driven by environment's crypto)
26
+ const id = cryptoAlea.string(16, alphanumeric);
29
27
  ```
30
28
  # Custom sources
31
29
 
@@ -64,5 +62,5 @@ const fast = mulberry32("my-seed");
64
62
  const varied = sfc32(1, 2, 3, 4);
65
63
  const strong = xoshiro128pp(5, 6, 7, 8);
66
64
 
67
- const reproducibleRoll = varied.roll(3, 6);
65
+ const reproducibleId = varied.string(12, hexadecimal);
68
66
  ```
package/entry/other.js CHANGED
@@ -4,5 +4,5 @@ export * from "./common.js";
4
4
  * An Alea instance that uses the runtime environment's `crypto` provider as a source
5
5
  */
6
6
  export const cryptoAlea = new Alea(() => {
7
- throw new Error("cryptoAlea is not available in this environment. Consider using createAleaFromByteSource() with your environment's crypto API.");
7
+ throw new Error("cryptoAlea is not available in this environment. Consider using aleaFromByteSource() with your environment's crypto API.");
8
8
  });
@@ -9,12 +9,6 @@ export declare class Alea {
9
9
  * @param next Source RNG - a function that returns a value >= 0 and < 1
10
10
  */
11
11
  constructor(next: RandomFunction);
12
- /**
13
- * Generate a series of normalised random values
14
- * @param count
15
- * @returns Random values
16
- */
17
- batch(count: number): number[];
18
12
  /**
19
13
  * Pick a random item from an array
20
14
  * @param items
@@ -62,7 +56,7 @@ export declare class Alea {
62
56
  * greeting: ["hello", "hi", "{int} blessings"],
63
57
  * addressee: ["world", "planet", "{adjective} @xtia user"],
64
58
  * adjective: ["beautiful", "wonderful"],
65
- * int: () => alea.int(0, 9).toString(),
59
+ * int: () => Math.floor(alea.between(3, 9)).toString(),
66
60
  * }, "{greeting}, {addressee}!")
67
61
  * ```
68
62
  * @param table
@@ -109,20 +103,6 @@ export declare class Alea {
109
103
  * @returns Gaussian normal pair
110
104
  */
111
105
  normal(mean?: number, deviation?: number): [number, number];
112
- /**
113
- * Get a random integer value between `min` and `max`, inclusive.
114
- * @param min Minimum value, **inclusive**
115
- * @param max Maximum value, **inclusive**
116
- * @returns Random int value
117
- */
118
- int(min: number, max: number): number;
119
- /**
120
- * Roll dice
121
- * @param count Number of dice to roll (default 1)
122
- * @param sides Number of sides per die (default 6)
123
- * @returns Dice result
124
- */
125
- roll(count?: number, sides?: number): number;
126
106
  /**
127
107
  * Generate a random UUID (version 4)
128
108
  *
package/internal/alea.js CHANGED
@@ -5,17 +5,6 @@ export class Alea {
5
5
  constructor(next) {
6
6
  this.next = next;
7
7
  }
8
- /**
9
- * Generate a series of normalised random values
10
- * @param count
11
- * @returns Random values
12
- */
13
- batch(count) {
14
- if (!Number.isInteger(count) || count < 0) {
15
- throw new RangeError("count must be a non-negative integer");
16
- }
17
- return Array.from({ length: count }, () => this.next());
18
- }
19
8
  sample(items, count) {
20
9
  if (count === undefined) {
21
10
  if (items.length === 0)
@@ -90,7 +79,7 @@ export class Alea {
90
79
  * greeting: ["hello", "hi", "{int} blessings"],
91
80
  * addressee: ["world", "planet", "{adjective} @xtia user"],
92
81
  * adjective: ["beautiful", "wonderful"],
93
- * int: () => alea.int(0, 9).toString(),
82
+ * int: () => Math.floor(alea.between(3, 9)).toString(),
94
83
  * }, "{greeting}, {addressee}!")
95
84
  * ```
96
85
  * @param table
@@ -157,10 +146,10 @@ export class Alea {
157
146
  const words = Math.floor(len / 4);
158
147
  const view = new DataView(byteArray.buffer, byteArray.byteOffset, byteArray.byteLength);
159
148
  for (let i = 0; i < words; i++) {
160
- view.setUint32(i * 4, this.int(0, 0xffffffff));
149
+ view.setUint32(i * 4, this.between(0, 0x100000000) >>> 0);
161
150
  }
162
151
  for (let i = words * 4; i < len; i++) {
163
- byteArray[i] = this.int(0, 255);
152
+ byteArray[i] = this.between(0, 256) | 0;
164
153
  }
165
154
  return result;
166
155
  }
@@ -199,28 +188,6 @@ export class Alea {
199
188
  mean + z1 * deviation
200
189
  ];
201
190
  }
202
- /**
203
- * Get a random integer value between `min` and `max`, inclusive.
204
- * @param min Minimum value, **inclusive**
205
- * @param max Maximum value, **inclusive**
206
- * @returns Random int value
207
- */
208
- int(min, max) {
209
- return Math.floor(this.between(min, max + 1));
210
- }
211
- /**
212
- * Roll dice
213
- * @param count Number of dice to roll (default 1)
214
- * @param sides Number of sides per die (default 6)
215
- * @returns Dice result
216
- */
217
- roll(count = 1, sides = 6) {
218
- let total = 0;
219
- for (let i = 0; i < count; i++) {
220
- total += this.int(1, sides);
221
- }
222
- return total;
223
- }
224
191
  /**
225
192
  * Generate a random UUID (version 4)
226
193
  *
@@ -4,8 +4,8 @@ import { Alea } from "./alea.js";
4
4
  * as a RNG source
5
5
  * @example
6
6
  * ```ts
7
- * const cryptoAlea = createAleaFromByteSource(crypto.getRandomValues);
8
- * const hwAlea = createAleaFromByteSource(hardwareRng.fillBytes);
7
+ * const cryptoAlea = aleaFromByteSource(crypto.getRandomValues);
8
+ * const hwAlea = aleaFromByteSource(hardwareRng.fillBytes);
9
9
  * ```
10
10
  * @param applyBytes A callback that fills a Uint8Array with random bytes
11
11
  * @returns A byte generator-sourced Alea instance
@@ -25,8 +25,8 @@ export declare function aleaFromSeed(seed: number | string): Alea;
25
25
  * Create an Alea instance using a custom function as an RNG source
26
26
  * @example
27
27
  * ```ts
28
- * const basicAlea = createAleaFromFunc(Math.random);
29
- * const lcgAlea = createAleaFromFunc(customRng.next);
28
+ * const basicAlea = aleaFromFunc(Math.random);
29
+ * const lcgAlea = aleaFromFunc(customRng.next);
30
30
  * ```
31
31
  * @param fn Source RNG; a function that returns a value >= 0 and < 1
32
32
  * @returns Custom function-sourced Alea instance
@@ -5,8 +5,8 @@ import { Alea } from "./alea.js";
5
5
  * as a RNG source
6
6
  * @example
7
7
  * ```ts
8
- * const cryptoAlea = createAleaFromByteSource(crypto.getRandomValues);
9
- * const hwAlea = createAleaFromByteSource(hardwareRng.fillBytes);
8
+ * const cryptoAlea = aleaFromByteSource(crypto.getRandomValues);
9
+ * const hwAlea = aleaFromByteSource(hardwareRng.fillBytes);
10
10
  * ```
11
11
  * @param applyBytes A callback that fills a Uint8Array with random bytes
12
12
  * @returns A byte generator-sourced Alea instance
@@ -36,8 +36,8 @@ export function aleaFromSeed(seed) {
36
36
  * Create an Alea instance using a custom function as an RNG source
37
37
  * @example
38
38
  * ```ts
39
- * const basicAlea = createAleaFromFunc(Math.random);
40
- * const lcgAlea = createAleaFromFunc(customRng.next);
39
+ * const basicAlea = aleaFromFunc(Math.random);
40
+ * const lcgAlea = aleaFromFunc(customRng.next);
41
41
  * ```
42
42
  * @param fn Source RNG; a function that returns a value >= 0 and < 1
43
43
  * @returns Custom function-sourced Alea instance
@@ -45,4 +45,4 @@ export function aleaFromSeed(seed) {
45
45
  export function aleaFromFunc(fn) {
46
46
  return new Alea(fn);
47
47
  }
48
- // const xkcdAlea = createAleaFromFunc(() => 4/6); // decided by die roll
48
+ // const xkcdAlea = aleaFromFunc(() => 4/6); // decided by die roll
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xtia/alea-rc",
3
- "version": "0.0.10",
3
+ "version": "0.0.12",
4
4
  "description": "RNG utilities",
5
5
  "repository": {
6
6
  "url": "https://github.com/tiadrop/alea",