@thi.ng/random 3.0.0 → 3.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/CHANGELOG.md CHANGED
@@ -3,6 +3,41 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
+ # [3.1.0](https://github.com/thi-ng/umbrella/compare/@thi.ng/random@3.0.3...@thi.ng/random@3.1.0) (2021-10-25)
7
+
8
+
9
+ ### Features
10
+
11
+ * **random:** add pickRandom() ([0899aed](https://github.com/thi-ng/umbrella/commit/0899aed89fd691df4ddc5273995a579414f506d8))
12
+
13
+
14
+
15
+
16
+
17
+ ## [3.0.3](https://github.com/thi-ng/umbrella/compare/@thi.ng/random@3.0.2...@thi.ng/random@3.0.3) (2021-10-15)
18
+
19
+ **Note:** Version bump only for package @thi.ng/random
20
+
21
+
22
+
23
+
24
+
25
+ ## [3.0.2](https://github.com/thi-ng/umbrella/compare/@thi.ng/random@3.0.1...@thi.ng/random@3.0.2) (2021-10-15)
26
+
27
+ **Note:** Version bump only for package @thi.ng/random
28
+
29
+
30
+
31
+
32
+
33
+ ## [3.0.1](https://github.com/thi-ng/umbrella/compare/@thi.ng/random@3.0.0...@thi.ng/random@3.0.1) (2021-10-13)
34
+
35
+ **Note:** Version bump only for package @thi.ng/random
36
+
37
+
38
+
39
+
40
+
6
41
  # [3.0.0](https://github.com/thi-ng/umbrella/compare/@thi.ng/random@2.4.8...@thi.ng/random@3.0.0) (2021-10-12)
7
42
 
8
43
 
package/README.md CHANGED
@@ -80,15 +80,16 @@ ES module import:
80
80
 
81
81
  [Skypack documentation](https://docs.skypack.dev/)
82
82
 
83
- For NodeJS (v14.6+):
83
+ For Node.js REPL:
84
84
 
85
85
  ```text
86
- node --experimental-specifier-resolution=node --experimental-repl-await
86
+ # with flag only for < v16
87
+ node --experimental-repl-await
87
88
 
88
89
  > const random = await import("@thi.ng/random");
89
90
  ```
90
91
 
91
- Package sizes (gzipped, pre-treeshake): ESM: 1.83 KB
92
+ Package sizes (gzipped, pre-treeshake): ESM: 1.86 KB
92
93
 
93
94
  ## Dependencies
94
95
 
package/arandom.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { IRandom } from "./api";
1
+ import type { IRandom } from "./api.js";
2
2
  export declare abstract class ARandom implements IRandom {
3
3
  abstract int(): number;
4
4
  float(norm?: number): number;
package/coin.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { IRandom } from "./api";
1
+ import type { IRandom } from "./api.js";
2
2
  /**
3
3
  * Returns true w/ a (theoretical) probability of 50% (obviously depending on
4
4
  * quality of given {@link IRandom}) PRNG.
package/coin.js CHANGED
@@ -1,4 +1,4 @@
1
- import { SYSTEM } from "./system";
1
+ import { SYSTEM } from "./system.js";
2
2
  /**
3
3
  * Returns true w/ a (theoretical) probability of 50% (obviously depending on
4
4
  * quality of given {@link IRandom}) PRNG.
package/crypto.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import type { IBuffered, ICopy } from "@thi.ng/api";
2
- import { ARandom } from "./arandom";
2
+ import { ARandom } from "./arandom.js";
3
3
  /**
4
4
  * Currently browser only, a `window.crypto` backed {@link IRandom}
5
5
  * implementation. Random values are buffered to minimize overhead. Buffer size
package/crypto.js CHANGED
@@ -1,5 +1,5 @@
1
- import { ARandom } from "./arandom";
2
- import { randomBytes } from "./random-bytes";
1
+ import { ARandom } from "./arandom.js";
2
+ import { randomBytes } from "./random-bytes.js";
3
3
  /**
4
4
  * Currently browser only, a `window.crypto` backed {@link IRandom}
5
5
  * implementation. Random values are buffered to minimize overhead. Buffer size
@@ -11,9 +11,6 @@ import { randomBytes } from "./random-bytes";
11
11
  *
12
12
  */
13
13
  export class Crypto extends ARandom {
14
- buffer;
15
- u32;
16
- i;
17
14
  /**
18
15
  * @param size - buffer size in bytes (will be rounded to next multiple of 4)
19
16
  */
@@ -1,4 +1,4 @@
1
- import type { IRandom } from "../api";
1
+ import type { IRandom } from "../api.js";
2
2
  /**
3
3
  * Higher order function. Returns no-arg function, yielding values in
4
4
  * exponential distribution based on given rate `lambda`.
@@ -1,4 +1,4 @@
1
- import { SYSTEM } from "../system";
1
+ import { SYSTEM } from "../system.js";
2
2
  /**
3
3
  * Higher order function. Returns no-arg function, yielding values in
4
4
  * exponential distribution based on given rate `lambda`.
@@ -1,4 +1,4 @@
1
- import type { IRandom } from "../api";
1
+ import type { IRandom } from "../api.js";
2
2
  /**
3
3
  * Higher order function. Takes a {@link IRandom} instance and returns a no-arg
4
4
  * function which produces values with approx. normal distribution using CLT
@@ -1,4 +1,4 @@
1
- import { SYSTEM } from "../system";
1
+ import { SYSTEM } from "../system.js";
2
2
  /**
3
3
  * Higher order function. Takes a {@link IRandom} instance and returns a no-arg
4
4
  * function which produces values with approx. normal distribution using CLT
@@ -1,4 +1,4 @@
1
- import type { IRandom } from "../api";
1
+ import type { IRandom } from "../api.js";
2
2
  /**
3
3
  * HOF. Returns zero-arg function, yielding values in geometric distribution,
4
4
  * aka the number of independent trials required for the first occurrence of
@@ -1,4 +1,4 @@
1
- import { SYSTEM } from "../system";
1
+ import { SYSTEM } from "../system.js";
2
2
  /**
3
3
  * HOF. Returns zero-arg function, yielding values in geometric distribution,
4
4
  * aka the number of independent trials required for the first occurrence of
@@ -1,4 +1,4 @@
1
- import type { IRandom } from "../api";
1
+ import type { IRandom } from "../api.js";
2
2
  /**
3
3
  * HOF. Returns zero-arg function, yielding values with normal distribution
4
4
  * for given `bias` and standard deviation `sigma`.
@@ -1,4 +1,4 @@
1
- import { SYSTEM } from "../system";
1
+ import { SYSTEM } from "../system.js";
2
2
  /**
3
3
  * HOF. Returns zero-arg function, yielding values with normal distribution
4
4
  * for given `bias` and standard deviation `sigma`.
@@ -1,4 +1,4 @@
1
- import type { IRandom } from "../api";
1
+ import type { IRandom } from "../api.js";
2
2
  /**
3
3
  * HOF. Returns zero-arg function, yielding uniformly distributed values in the
4
4
  * `[min,max)` interval (default: `[0,1)`).
@@ -1,4 +1,4 @@
1
- import { SYSTEM } from "../system";
1
+ import { SYSTEM } from "../system.js";
2
2
  /**
3
3
  * HOF. Returns zero-arg function, yielding uniformly distributed values in the
4
4
  * `[min,max)` interval (default: `[0,1)`).
package/index.d.ts CHANGED
@@ -1,21 +1,22 @@
1
- export * from "./api";
2
- export * from "./arandom";
3
- export * from "./crypto";
4
- export * from "./smush32";
5
- export * from "./system";
6
- export * from "./xoshiro128";
7
- export * from "./xorshift128";
8
- export * from "./xorwow";
9
- export * from "./xsadd";
10
- export * from "./coin";
11
- export * from "./random-bytes";
12
- export * from "./random-id";
13
- export * from "./unique-indices";
14
- export * from "./uuid";
15
- export * from "./weighted-random";
16
- export * from "./distributions/exponential";
17
- export * from "./distributions/gaussian";
18
- export * from "./distributions/geometric";
19
- export * from "./distributions/normal";
20
- export * from "./distributions/uniform";
1
+ export * from "./api.js";
2
+ export * from "./arandom.js";
3
+ export * from "./coin.js";
4
+ export * from "./crypto.js";
5
+ export * from "./distributions/exponential.js";
6
+ export * from "./distributions/gaussian.js";
7
+ export * from "./distributions/geometric.js";
8
+ export * from "./distributions/normal.js";
9
+ export * from "./distributions/uniform.js";
10
+ export * from "./pick-random.js";
11
+ export * from "./random-bytes.js";
12
+ export * from "./random-id.js";
13
+ export * from "./smush32.js";
14
+ export * from "./system.js";
15
+ export * from "./unique-indices.js";
16
+ export * from "./uuid.js";
17
+ export * from "./weighted-random.js";
18
+ export * from "./xorshift128.js";
19
+ export * from "./xorwow.js";
20
+ export * from "./xoshiro128.js";
21
+ export * from "./xsadd.js";
21
22
  //# sourceMappingURL=index.d.ts.map
package/index.js CHANGED
@@ -1,20 +1,21 @@
1
- export * from "./api";
2
- export * from "./arandom";
3
- export * from "./crypto";
4
- export * from "./smush32";
5
- export * from "./system";
6
- export * from "./xoshiro128";
7
- export * from "./xorshift128";
8
- export * from "./xorwow";
9
- export * from "./xsadd";
10
- export * from "./coin";
11
- export * from "./random-bytes";
12
- export * from "./random-id";
13
- export * from "./unique-indices";
14
- export * from "./uuid";
15
- export * from "./weighted-random";
16
- export * from "./distributions/exponential";
17
- export * from "./distributions/gaussian";
18
- export * from "./distributions/geometric";
19
- export * from "./distributions/normal";
20
- export * from "./distributions/uniform";
1
+ export * from "./api.js";
2
+ export * from "./arandom.js";
3
+ export * from "./coin.js";
4
+ export * from "./crypto.js";
5
+ export * from "./distributions/exponential.js";
6
+ export * from "./distributions/gaussian.js";
7
+ export * from "./distributions/geometric.js";
8
+ export * from "./distributions/normal.js";
9
+ export * from "./distributions/uniform.js";
10
+ export * from "./pick-random.js";
11
+ export * from "./random-bytes.js";
12
+ export * from "./random-id.js";
13
+ export * from "./smush32.js";
14
+ export * from "./system.js";
15
+ export * from "./unique-indices.js";
16
+ export * from "./uuid.js";
17
+ export * from "./weighted-random.js";
18
+ export * from "./xorshift128.js";
19
+ export * from "./xorwow.js";
20
+ export * from "./xoshiro128.js";
21
+ export * from "./xsadd.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@thi.ng/random",
3
- "version": "3.0.0",
3
+ "version": "3.1.0",
4
4
  "description": "Pseudo-random number generators w/ unified API, distributions, weighted choices, ID generation",
5
5
  "type": "module",
6
6
  "module": "./index.js",
@@ -34,13 +34,13 @@
34
34
  "test": "testament test"
35
35
  },
36
36
  "dependencies": {
37
- "@thi.ng/api": "^8.0.0",
38
- "@thi.ng/checks": "^3.0.0",
39
- "@thi.ng/errors": "^2.0.0",
40
- "@thi.ng/hex": "^2.0.0"
37
+ "@thi.ng/api": "^8.0.4",
38
+ "@thi.ng/checks": "^3.0.4",
39
+ "@thi.ng/errors": "^2.0.4",
40
+ "@thi.ng/hex": "^2.0.4"
41
41
  },
42
42
  "devDependencies": {
43
- "@thi.ng/testament": "^0.1.0"
43
+ "@thi.ng/testament": "^0.1.4"
44
44
  },
45
45
  "keywords": [
46
46
  "binary",
@@ -63,6 +63,9 @@
63
63
  "publishConfig": {
64
64
  "access": "public"
65
65
  },
66
+ "engines": {
67
+ "node": ">=12.7"
68
+ },
66
69
  "files": [
67
70
  "*.js",
68
71
  "*.d.ts",
@@ -102,6 +105,9 @@
102
105
  "./distributions/uniform": {
103
106
  "import": "./distributions/uniform.js"
104
107
  },
108
+ "./pick-random": {
109
+ "import": "./pick-random.js"
110
+ },
105
111
  "./random-bytes": {
106
112
  "import": "./random-bytes.js"
107
113
  },
@@ -142,5 +148,5 @@
142
148
  "ksuid"
143
149
  ]
144
150
  },
145
- "gitHead": "9ac1344b38b565eb894306fbf72233b6c0b2d115"
151
+ "gitHead": "9ff00a103f76cc4917ef3f244132e218f2300a05"
146
152
  }
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Returns a random element from `src` using given {@link IRandom} instance
3
+ * (default: {@link SYSTEM}). The index selection will be constrained to the
4
+ * `[start,end)` interval (default: entire array).
5
+ *
6
+ * @param src
7
+ * @param rnd
8
+ * @param start
9
+ * @param end
10
+ */
11
+ export declare const pickRandom: <T>(src: ArrayLike<T>, rnd?: import("./system.js").SystemRandom, start?: number, end?: number) => T;
12
+ //# sourceMappingURL=pick-random.d.ts.map
package/pick-random.js ADDED
@@ -0,0 +1,12 @@
1
+ import { SYSTEM } from "./system.js";
2
+ /**
3
+ * Returns a random element from `src` using given {@link IRandom} instance
4
+ * (default: {@link SYSTEM}). The index selection will be constrained to the
5
+ * `[start,end)` interval (default: entire array).
6
+ *
7
+ * @param src
8
+ * @param rnd
9
+ * @param start
10
+ * @param end
11
+ */
12
+ export const pickRandom = (src, rnd = SYSTEM, start = 0, end = src.length) => src[rnd.minmax(start, end) | 0];
package/random-bytes.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { IRandom } from "./api";
1
+ import type { IRandom } from "./api.js";
2
2
  /**
3
3
  * Fills given byte array with random values sourced from given {@link IRandom}
4
4
  * instance.
package/random-bytes.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { hasCrypto } from "@thi.ng/checks/has-crypto";
2
- import { SYSTEM } from "./system";
2
+ import { SYSTEM } from "./system.js";
3
3
  /**
4
4
  * Fills given byte array with random values sourced from given {@link IRandom}
5
5
  * instance.
package/random-id.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { IRandom } from "./api";
1
+ import type { IRandom } from "./api.js";
2
2
  /**
3
3
  * Generates and returns a random string of `len` characters (default 4), plus
4
4
  * optional given `prefix` and using only provided `syms` characters (default
package/random-id.js CHANGED
@@ -1,4 +1,4 @@
1
- import { SYSTEM } from "./system";
1
+ import { SYSTEM } from "./system.js";
2
2
  /**
3
3
  * Generates and returns a random string of `len` characters (default 4), plus
4
4
  * optional given `prefix` and using only provided `syms` characters (default
package/smush32.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import type { IBuffered, ICopy } from "@thi.ng/api";
2
- import type { ISeedable } from "./api";
3
- import { ARandom } from "./arandom";
2
+ import type { ISeedable } from "./api.js";
3
+ import { ARandom } from "./arandom.js";
4
4
  /**
5
5
  * @remarks
6
6
  * References:
package/smush32.js CHANGED
@@ -1,5 +1,5 @@
1
- import { ARandom } from "./arandom";
2
- import { DEFAULT_SEED_32 } from "./constants";
1
+ import { ARandom } from "./arandom.js";
2
+ import { DEFAULT_SEED_32 } from "./constants.js";
3
3
  /**
4
4
  * @remarks
5
5
  * References:
@@ -8,7 +8,6 @@ import { DEFAULT_SEED_32 } from "./constants";
8
8
  * - https://gist.github.com/voidqk/d112165a26b45244a65298933c0349a4
9
9
  */
10
10
  export class Smush32 extends ARandom {
11
- buffer;
12
11
  constructor(seed = DEFAULT_SEED_32) {
13
12
  super();
14
13
  this.buffer = new Uint32Array([seed, 0]);
package/system.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { ARandom } from "./arandom";
1
+ import { ARandom } from "./arandom.js";
2
2
  /**
3
3
  * A `Math.random()` based {@link IRandom} implementation. Also @see
4
4
  * {@link SYSTEM}.
package/system.js CHANGED
@@ -1,4 +1,4 @@
1
- import { ARandom } from "./arandom";
1
+ import { ARandom } from "./arandom.js";
2
2
  const random = Math.random;
3
3
  /**
4
4
  * A `Math.random()` based {@link IRandom} implementation. Also @see
@@ -1,5 +1,5 @@
1
1
  import type { Fn0 } from "@thi.ng/api";
2
- import type { IRandom } from "./api";
2
+ import type { IRandom } from "./api.js";
3
3
  /**
4
4
  * Attempts to draw `k` unique values from given zero-arg function `fn`
5
5
  * (presumably a PRNG of sorts) and adds them to `existing` array of unique
package/unique-indices.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { assert } from "@thi.ng/errors/assert";
2
- import { SYSTEM } from "./system";
2
+ import { SYSTEM } from "./system.js";
3
3
  /**
4
4
  * Attempts to draw `k` unique values from given zero-arg function `fn`
5
5
  * (presumably a PRNG of sorts) and adds them to `existing` array of unique
package/uuid.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { IRandom } from "./api";
1
+ import type { IRandom } from "./api.js";
2
2
  /**
3
3
  * Depending on if `rnd` is given, uses {@link randomBytesFrom} or
4
4
  * {@link randomBytes} to fill given (optional) byte array with a new UUIDv4.
package/uuid.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { uuid as $uuid } from "@thi.ng/hex";
2
- import { randomBytes, randomBytesFrom } from "./random-bytes";
2
+ import { randomBytes, randomBytesFrom } from "./random-bytes.js";
3
3
  /**
4
4
  * Depending on if `rnd` is given, uses {@link randomBytesFrom} or
5
5
  * {@link randomBytes} to fill given (optional) byte array with a new UUIDv4.
@@ -1,4 +1,4 @@
1
- import type { IRandom } from "./api";
1
+ import type { IRandom } from "./api.js";
2
2
  /**
3
3
  * Returns a no-arg function which produces a random choice of given weighted
4
4
  * `choices` and using given {@link IRandom} instance (default {@link SYSTEM}.
@@ -1,5 +1,5 @@
1
1
  import { assert } from "@thi.ng/errors/assert";
2
- import { SYSTEM } from "./system";
2
+ import { SYSTEM } from "./system.js";
3
3
  /**
4
4
  * Returns a no-arg function which produces a random choice of given weighted
5
5
  * `choices` and using given {@link IRandom} instance (default {@link SYSTEM}.
package/xorshift128.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import type { IBuffered, ICopy } from "@thi.ng/api";
2
- import type { ISeedable } from "./api";
3
- import { ARandom } from "./arandom";
2
+ import type { ISeedable } from "./api.js";
3
+ import { ARandom } from "./arandom.js";
4
4
  /**
5
5
  * @remarks
6
6
  * Reference: https://en.wikipedia.org/wiki/Xorshift
package/xorshift128.js CHANGED
@@ -1,11 +1,10 @@
1
- import { ARandom } from "./arandom";
2
- import { DEFAULT_SEED_128 } from "./constants";
1
+ import { ARandom } from "./arandom.js";
2
+ import { DEFAULT_SEED_128 } from "./constants.js";
3
3
  /**
4
4
  * @remarks
5
5
  * Reference: https://en.wikipedia.org/wiki/Xorshift
6
6
  */
7
7
  export class XorShift128 extends ARandom {
8
- buffer;
9
8
  constructor(seed = DEFAULT_SEED_128) {
10
9
  super();
11
10
  this.buffer = new Uint32Array(4);
package/xorwow.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import type { IBuffered, ICopy } from "@thi.ng/api";
2
- import type { ISeedable } from "./api";
3
- import { ARandom } from "./arandom";
2
+ import type { ISeedable } from "./api.js";
3
+ import { ARandom } from "./arandom.js";
4
4
  /**
5
5
  * @remarks
6
6
  * Reference: https://en.wikipedia.org/wiki/Xorshift#xorwow
package/xorwow.js CHANGED
@@ -1,11 +1,10 @@
1
- import { ARandom } from "./arandom";
2
- import { DEFAULT_SEED_160 } from "./constants";
1
+ import { ARandom } from "./arandom.js";
2
+ import { DEFAULT_SEED_160 } from "./constants.js";
3
3
  /**
4
4
  * @remarks
5
5
  * Reference: https://en.wikipedia.org/wiki/Xorshift#xorwow
6
6
  */
7
7
  export class XorWow extends ARandom {
8
- buffer;
9
8
  constructor(seed = DEFAULT_SEED_160) {
10
9
  super();
11
10
  this.buffer = new Uint32Array(5);
package/xoshiro128.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import type { IBuffered, ICopy } from "@thi.ng/api";
2
- import type { ISeedable } from "./api";
3
- import { ARandom } from "./arandom";
2
+ import type { ISeedable } from "./api.js";
3
+ import { ARandom } from "./arandom.js";
4
4
  /**
5
5
  * @remarks
6
6
  * References:
package/xoshiro128.js CHANGED
@@ -1,5 +1,5 @@
1
- import { ARandom } from "./arandom";
2
- import { DEFAULT_SEED_128 } from "./constants";
1
+ import { ARandom } from "./arandom.js";
2
+ import { DEFAULT_SEED_128 } from "./constants.js";
3
3
  /**
4
4
  * @remarks
5
5
  * References:
@@ -7,7 +7,6 @@ import { DEFAULT_SEED_128 } from "./constants";
7
7
  * - http://prng.di.unimi.it/xoshiro128plusplus.c
8
8
  */
9
9
  export class Xoshiro128 extends ARandom {
10
- buffer;
11
10
  constructor(seed = DEFAULT_SEED_128) {
12
11
  super();
13
12
  this.buffer = new Uint32Array(4);
package/xsadd.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import type { IBuffered, ICopy } from "@thi.ng/api";
2
- import type { ISeedable } from "./api";
3
- import { ARandom } from "./arandom";
2
+ import type { ISeedable } from "./api.js";
3
+ import { ARandom } from "./arandom.js";
4
4
  /**
5
5
  * @remarks
6
6
  * Reference: https://github.com/MersenneTwister-Lab/XSadd/blob/develop/xsadd.h
package/xsadd.js CHANGED
@@ -1,11 +1,10 @@
1
- import { ARandom } from "./arandom";
2
- import { DEFAULT_SEED_32 } from "./constants";
1
+ import { ARandom } from "./arandom.js";
2
+ import { DEFAULT_SEED_32 } from "./constants.js";
3
3
  /**
4
4
  * @remarks
5
5
  * Reference: https://github.com/MersenneTwister-Lab/XSadd/blob/develop/xsadd.h
6
6
  */
7
7
  export class XsAdd extends ARandom {
8
- buffer;
9
8
  constructor(seed = DEFAULT_SEED_32) {
10
9
  super();
11
10
  this.buffer = new Uint32Array(4);