koota-kit 0.3.1 → 0.4.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
@@ -1,5 +1,12 @@
1
1
  # Changelog
2
2
 
3
+ ## [0.4.0](https://github.com/jbcom/koota-kit/compare/v0.3.1...v0.4.0) (2026-10-08)
4
+
5
+
6
+ ### Features
7
+
8
+ * **rng:** pick and shuffle over a stream ([167eb1c](https://github.com/jbcom/koota-kit/commit/167eb1c75fdb7aa2b78f9d9d4e2533c81e0b5230))
9
+
3
10
  ## [0.3.1](https://github.com/jbcom/koota-kit/compare/v0.3.0...v0.3.1) (2026-10-07)
4
11
 
5
12
 
@@ -12,7 +12,7 @@
12
12
  // ./eventLog — scratch-backed publish/drain event logs with a peek seam for
13
13
  // observers (harnesses, tests, HUDs)
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
- exports.defineTrait = exports.defineEventLog = exports.MASTER_SEED_BYTES = exports.isMasterSeed = exports.deriveSeed = exports.createMasterSeed = exports.substream = exports.snapshotSubstreams = exports.snapshotStream = exports.snapshotLayers = exports.restoreSubstreams = exports.restoreStream = exports.restoreLayers = exports.nextU32 = exports.nextInt = exports.nextFloat = exports.getSubstream = exports.createSubstreams = exports.createRng = exports.chance = exports.trait = exports.snapshotWorld = exports.restoreWorldHeader = exports.relation = exports.destroySimWorld = exports.createSimWorld = exports.createActions = exports.advanceClock = void 0;
15
+ exports.defineTrait = exports.defineEventLog = exports.MASTER_SEED_BYTES = exports.isMasterSeed = exports.deriveSeed = exports.createMasterSeed = exports.substream = exports.snapshotSubstreams = exports.snapshotStream = exports.snapshotLayers = exports.shuffle = exports.restoreSubstreams = exports.restoreStream = exports.restoreLayers = exports.pick = exports.nextU32 = exports.nextInt = exports.nextFloat = exports.getSubstream = exports.createSubstreams = exports.createRng = exports.chance = exports.trait = exports.snapshotWorld = exports.restoreWorldHeader = exports.relation = exports.destroySimWorld = exports.createSimWorld = exports.createActions = exports.advanceClock = void 0;
16
16
  var world_js_1 = require("./world.cjs");
17
17
  Object.defineProperty(exports, "advanceClock", { enumerable: true, get: function () { return world_js_1.advanceClock; } });
18
18
  Object.defineProperty(exports, "createActions", { enumerable: true, get: function () { return world_js_1.createActions; } });
@@ -30,9 +30,11 @@ Object.defineProperty(exports, "getSubstream", { enumerable: true, get: function
30
30
  Object.defineProperty(exports, "nextFloat", { enumerable: true, get: function () { return rng_js_1.nextFloat; } });
31
31
  Object.defineProperty(exports, "nextInt", { enumerable: true, get: function () { return rng_js_1.nextInt; } });
32
32
  Object.defineProperty(exports, "nextU32", { enumerable: true, get: function () { return rng_js_1.nextU32; } });
33
+ Object.defineProperty(exports, "pick", { enumerable: true, get: function () { return rng_js_1.pick; } });
33
34
  Object.defineProperty(exports, "restoreLayers", { enumerable: true, get: function () { return rng_js_1.restoreLayers; } });
34
35
  Object.defineProperty(exports, "restoreStream", { enumerable: true, get: function () { return rng_js_1.restoreStream; } });
35
36
  Object.defineProperty(exports, "restoreSubstreams", { enumerable: true, get: function () { return rng_js_1.restoreSubstreams; } });
37
+ Object.defineProperty(exports, "shuffle", { enumerable: true, get: function () { return rng_js_1.shuffle; } });
36
38
  Object.defineProperty(exports, "snapshotLayers", { enumerable: true, get: function () { return rng_js_1.snapshotLayers; } });
37
39
  Object.defineProperty(exports, "snapshotStream", { enumerable: true, get: function () { return rng_js_1.snapshotStream; } });
38
40
  Object.defineProperty(exports, "snapshotSubstreams", { enumerable: true, get: function () { return rng_js_1.snapshotSubstreams; } });
@@ -1,6 +1,6 @@
1
1
  export { advanceClock, createActions, createSimWorld, destroySimWorld, relation, restoreWorldHeader, snapshotWorld, trait, } from "./world.cjs";
2
2
  export type { Entity, World, WorldHandle, WorldSnapshot } from "./world.cjs";
3
- export { chance, createRng, createSubstreams, getSubstream, nextFloat, nextInt, nextU32, restoreLayers, restoreStream, restoreSubstreams, snapshotLayers, snapshotStream, snapshotSubstreams, substream, } from "./rng.cjs";
3
+ export { chance, createRng, createSubstreams, getSubstream, nextFloat, nextInt, nextU32, pick, restoreLayers, restoreStream, restoreSubstreams, shuffle, snapshotLayers, snapshotStream, snapshotSubstreams, substream, } from "./rng.cjs";
4
4
  export type { RngLayers, RngLayersSnapshot, RngSeeds, RngStream, RngStreamSnapshot, Substreams, SubstreamsSnapshot, } from "./rng.cjs";
5
5
  export { createMasterSeed, deriveSeed, isMasterSeed, MASTER_SEED_BYTES } from "./seed.cjs";
6
6
  export type { EntropySource, SeedPart } from "./seed.cjs";
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAaA,OAAO,EACL,YAAY,EACZ,aAAa,EACb,cAAc,EACd,eAAe,EACf,QAAQ,EACR,kBAAkB,EAClB,aAAa,EACb,KAAK,GACN,MAAM,YAAY,CAAC;AACpB,YAAY,EAAE,MAAM,EAAE,KAAK,EAAE,WAAW,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AAE5E,OAAO,EACL,MAAM,EACN,SAAS,EACT,gBAAgB,EAChB,YAAY,EACZ,SAAS,EACT,OAAO,EACP,OAAO,EACP,aAAa,EACb,aAAa,EACb,iBAAiB,EACjB,cAAc,EACd,cAAc,EACd,kBAAkB,EAClB,SAAS,GACV,MAAM,UAAU,CAAC;AAClB,YAAY,EACV,SAAS,EACT,iBAAiB,EACjB,QAAQ,EACR,SAAS,EACT,iBAAiB,EACjB,UAAU,EACV,kBAAkB,GACnB,MAAM,UAAU,CAAC;AAElB,OAAO,EAAE,gBAAgB,EAAE,UAAU,EAAE,YAAY,EAAE,iBAAiB,EAAE,MAAM,WAAW,CAAC;AAC1F,YAAY,EAAE,aAAa,EAAE,QAAQ,EAAE,MAAM,WAAW,CAAC;AAEzD,OAAO,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AAC/C,YAAY,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AAE9C,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAChD,YAAY,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAaA,OAAO,EACL,YAAY,EACZ,aAAa,EACb,cAAc,EACd,eAAe,EACf,QAAQ,EACR,kBAAkB,EAClB,aAAa,EACb,KAAK,GACN,MAAM,YAAY,CAAC;AACpB,YAAY,EAAE,MAAM,EAAE,KAAK,EAAE,WAAW,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AAE5E,OAAO,EACL,MAAM,EACN,SAAS,EACT,gBAAgB,EAChB,YAAY,EACZ,SAAS,EACT,OAAO,EACP,OAAO,EACP,IAAI,EACJ,aAAa,EACb,aAAa,EACb,iBAAiB,EACjB,OAAO,EACP,cAAc,EACd,cAAc,EACd,kBAAkB,EAClB,SAAS,GACV,MAAM,UAAU,CAAC;AAClB,YAAY,EACV,SAAS,EACT,iBAAiB,EACjB,QAAQ,EACR,SAAS,EACT,iBAAiB,EACjB,UAAU,EACV,kBAAkB,GACnB,MAAM,UAAU,CAAC;AAElB,OAAO,EAAE,gBAAgB,EAAE,UAAU,EAAE,YAAY,EAAE,iBAAiB,EAAE,MAAM,WAAW,CAAC;AAC1F,YAAY,EAAE,aAAa,EAAE,QAAQ,EAAE,MAAM,WAAW,CAAC;AAEzD,OAAO,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AAC/C,YAAY,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AAE9C,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAChD,YAAY,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC"}
package/dist/cjs/rng.cjs CHANGED
@@ -28,6 +28,8 @@ exports.nextU32 = nextU32;
28
28
  exports.nextFloat = nextFloat;
29
29
  exports.nextInt = nextInt;
30
30
  exports.chance = chance;
31
+ exports.pick = pick;
32
+ exports.shuffle = shuffle;
31
33
  exports.snapshotStream = snapshotStream;
32
34
  exports.snapshotLayers = snapshotLayers;
33
35
  exports.restoreStream = restoreStream;
@@ -118,6 +120,33 @@ function chance(stream, p) {
118
120
  }
119
121
  return nextFloat(stream) < p;
120
122
  }
123
+ /**
124
+ * Pick one element of `items` uniformly. Valid calls consume exactly one
125
+ * `nextInt` draw; an empty array is rejected without consuming the stream,
126
+ * so a caller can never silently skip a draw it meant to make.
127
+ */
128
+ function pick(stream, items) {
129
+ if (items.length === 0) {
130
+ throw new RangeError("pick: items must not be empty.");
131
+ }
132
+ return items[nextInt(stream, 0, items.length)];
133
+ }
134
+ /**
135
+ * Return a uniformly shuffled copy of `items` (Fisher–Yates, from the last
136
+ * index down). The input is never mutated. Valid calls consume exactly
137
+ * `items.length - 1` draws (none for zero or one element), so the draw count
138
+ * depends only on the length — replay stays aligned whatever the contents.
139
+ */
140
+ function shuffle(stream, items) {
141
+ const out = [...items];
142
+ for (let i = out.length - 1; i > 0; i--) {
143
+ const j = nextInt(stream, 0, i + 1);
144
+ const swap = out[i];
145
+ out[i] = out[j];
146
+ out[j] = swap;
147
+ }
148
+ return out;
149
+ }
121
150
  /**
122
151
  * Capture `stream`'s internal seedrandom state as a plain, JSON-serializable
123
152
  * object (deep-cloned, so later draws on `stream` cannot mutate the
@@ -50,6 +50,19 @@ export declare function nextInt(stream: RngStream, minInclusive: number, maxExcl
50
50
  * deterministic across codepaths that must replay identically.
51
51
  */
52
52
  export declare function chance(stream: RngStream, p: number): boolean;
53
+ /**
54
+ * Pick one element of `items` uniformly. Valid calls consume exactly one
55
+ * `nextInt` draw; an empty array is rejected without consuming the stream,
56
+ * so a caller can never silently skip a draw it meant to make.
57
+ */
58
+ export declare function pick<T>(stream: RngStream, items: readonly T[]): T;
59
+ /**
60
+ * Return a uniformly shuffled copy of `items` (Fisher–Yates, from the last
61
+ * index down). The input is never mutated. Valid calls consume exactly
62
+ * `items.length - 1` draws (none for zero or one element), so the draw count
63
+ * depends only on the length — replay stays aligned whatever the contents.
64
+ */
65
+ export declare function shuffle<T>(stream: RngStream, items: readonly T[]): T[];
53
66
  export type RngStreamSnapshot = {
54
67
  state: State;
55
68
  };
@@ -1 +1 @@
1
- {"version":3,"file":"rng.d.ts","sourceRoot":"","sources":["../../src/rng.ts"],"names":[],"mappings":"AAqBA,OAAO,UAAU,MAAM,YAAY,CAAC;AAGpC,KAAK,KAAK,GAAG,UAAU,CAAC,KAAK,CAAC,IAAI,CAAC;AACnC,KAAK,QAAQ,GAAG,UAAU,CAAC,YAAY,CAAC,KAAK,CAAC,CAAC;AAE/C,MAAM,MAAM,SAAS,GAAG;IACtB,IAAI,EAAE,QAAQ,CAAC;CAChB,CAAC;AAEF,MAAM,MAAM,SAAS,GAAG;IACtB,GAAG,EAAE,SAAS,CAAC;IACf,MAAM,EAAE,SAAS,CAAC;CACnB,CAAC;AAEF,MAAM,MAAM,QAAQ,GAAG;IACrB,uEAAuE;IACvE,QAAQ,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAAC;IAC9B,8FAA8F;IAC9F,QAAQ,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAAC;CAClC,CAAC;AAgBF;;;;;GAKG;AACH,wBAAgB,SAAS,CAAC,KAAK,EAAE,QAAQ,GAAG,SAAS,CAUpD;AAED;;;;;;GAMG;AACH,wBAAgB,OAAO,CAAC,MAAM,EAAE,SAAS,GAAG,MAAM,CAEjD;AAED;;;;GAIG;AACH,wBAAgB,SAAS,CAAC,MAAM,EAAE,SAAS,GAAG,MAAM,CAEnD;AAED;;;;;GAKG;AACH,wBAAgB,OAAO,CAAC,MAAM,EAAE,SAAS,EAAE,YAAY,EAAE,MAAM,EAAE,YAAY,EAAE,MAAM,GAAG,MAAM,CAc7F;AAED;;;;;;GAMG;AACH,wBAAgB,MAAM,CAAC,MAAM,EAAE,SAAS,EAAE,CAAC,EAAE,MAAM,GAAG,OAAO,CAK5D;AAED,MAAM,MAAM,iBAAiB,GAAG;IAAE,KAAK,EAAE,KAAK,CAAA;CAAE,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AE,UAAU,EAAE,IAAI,EAAE,MAAM,GAAG,SAAS,CAO5E;AAED;;;GAGG;AACH,wBAAgB,kBAAkB,CAAC,UAAU,EAAE,UAAU,GAAG,kBAAkB,CAW7E;AAED;;;;GAIG;AACH,wBAAgB,iBAAiB,CAAC,QAAQ,EAAE,kBAAkB,GAAG,UAAU,CAiB1E"}
@@ -1,6 +1,6 @@
1
1
  export { advanceClock, createActions, createSimWorld, destroySimWorld, relation, restoreWorldHeader, snapshotWorld, trait, } from "./world.js";
2
2
  export type { Entity, World, WorldHandle, WorldSnapshot } from "./world.js";
3
- export { chance, createRng, createSubstreams, getSubstream, nextFloat, nextInt, nextU32, restoreLayers, restoreStream, restoreSubstreams, snapshotLayers, snapshotStream, snapshotSubstreams, substream, } from "./rng.js";
3
+ export { chance, createRng, createSubstreams, getSubstream, nextFloat, nextInt, nextU32, pick, restoreLayers, restoreStream, restoreSubstreams, shuffle, snapshotLayers, snapshotStream, snapshotSubstreams, substream, } from "./rng.js";
4
4
  export type { RngLayers, RngLayersSnapshot, RngSeeds, RngStream, RngStreamSnapshot, Substreams, SubstreamsSnapshot, } from "./rng.js";
5
5
  export { createMasterSeed, deriveSeed, isMasterSeed, MASTER_SEED_BYTES } from "./seed.js";
6
6
  export type { EntropySource, SeedPart } from "./seed.js";
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAaA,OAAO,EACL,YAAY,EACZ,aAAa,EACb,cAAc,EACd,eAAe,EACf,QAAQ,EACR,kBAAkB,EAClB,aAAa,EACb,KAAK,GACN,MAAM,YAAY,CAAC;AACpB,YAAY,EAAE,MAAM,EAAE,KAAK,EAAE,WAAW,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AAE5E,OAAO,EACL,MAAM,EACN,SAAS,EACT,gBAAgB,EAChB,YAAY,EACZ,SAAS,EACT,OAAO,EACP,OAAO,EACP,aAAa,EACb,aAAa,EACb,iBAAiB,EACjB,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EAAE,MAAM,WAAW,CAAC;AAC1F,YAAY,EAAE,aAAa,EAAE,QAAQ,EAAE,MAAM,WAAW,CAAC;AAEzD,OAAO,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AAC/C,YAAY,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AAE9C,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAChD,YAAY,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC"}
package/dist/esm/index.js CHANGED
@@ -11,7 +11,7 @@
11
11
  // ./eventLog — scratch-backed publish/drain event logs with a peek seam for
12
12
  // observers (harnesses, tests, HUDs)
13
13
  export { advanceClock, createActions, createSimWorld, destroySimWorld, relation, restoreWorldHeader, snapshotWorld, trait, } from "./world.js";
14
- export { chance, createRng, createSubstreams, getSubstream, nextFloat, nextInt, nextU32, restoreLayers, restoreStream, restoreSubstreams, snapshotLayers, snapshotStream, snapshotSubstreams, substream, } from "./rng.js";
14
+ export { chance, createRng, createSubstreams, getSubstream, nextFloat, nextInt, nextU32, pick, restoreLayers, restoreStream, restoreSubstreams, shuffle, snapshotLayers, snapshotStream, snapshotSubstreams, substream, } from "./rng.js";
15
15
  export { createMasterSeed, deriveSeed, isMasterSeed, MASTER_SEED_BYTES } from "./seed.js";
16
16
  export { defineEventLog } from "./eventLog.js";
17
17
  export { defineTrait } from "./traits/index.js";
package/dist/esm/rng.d.ts CHANGED
@@ -50,6 +50,19 @@ export declare function nextInt(stream: RngStream, minInclusive: number, maxExcl
50
50
  * deterministic across codepaths that must replay identically.
51
51
  */
52
52
  export declare function chance(stream: RngStream, p: number): boolean;
53
+ /**
54
+ * Pick one element of `items` uniformly. Valid calls consume exactly one
55
+ * `nextInt` draw; an empty array is rejected without consuming the stream,
56
+ * so a caller can never silently skip a draw it meant to make.
57
+ */
58
+ export declare function pick<T>(stream: RngStream, items: readonly T[]): T;
59
+ /**
60
+ * Return a uniformly shuffled copy of `items` (Fisher–Yates, from the last
61
+ * index down). The input is never mutated. Valid calls consume exactly
62
+ * `items.length - 1` draws (none for zero or one element), so the draw count
63
+ * depends only on the length — replay stays aligned whatever the contents.
64
+ */
65
+ export declare function shuffle<T>(stream: RngStream, items: readonly T[]): T[];
53
66
  export type RngStreamSnapshot = {
54
67
  state: State;
55
68
  };
@@ -1 +1 @@
1
- {"version":3,"file":"rng.d.ts","sourceRoot":"","sources":["../../src/rng.ts"],"names":[],"mappings":"AAqBA,OAAO,UAAU,MAAM,YAAY,CAAC;AAGpC,KAAK,KAAK,GAAG,UAAU,CAAC,KAAK,CAAC,IAAI,CAAC;AACnC,KAAK,QAAQ,GAAG,UAAU,CAAC,YAAY,CAAC,KAAK,CAAC,CAAC;AAE/C,MAAM,MAAM,SAAS,GAAG;IACtB,IAAI,EAAE,QAAQ,CAAC;CAChB,CAAC;AAEF,MAAM,MAAM,SAAS,GAAG;IACtB,GAAG,EAAE,SAAS,CAAC;IACf,MAAM,EAAE,SAAS,CAAC;CACnB,CAAC;AAEF,MAAM,MAAM,QAAQ,GAAG;IACrB,uEAAuE;IACvE,QAAQ,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAAC;IAC9B,8FAA8F;IAC9F,QAAQ,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAAC;CAClC,CAAC;AAgBF;;;;;GAKG;AACH,wBAAgB,SAAS,CAAC,KAAK,EAAE,QAAQ,GAAG,SAAS,CAUpD;AAED;;;;;;GAMG;AACH,wBAAgB,OAAO,CAAC,MAAM,EAAE,SAAS,GAAG,MAAM,CAEjD;AAED;;;;GAIG;AACH,wBAAgB,SAAS,CAAC,MAAM,EAAE,SAAS,GAAG,MAAM,CAEnD;AAED;;;;;GAKG;AACH,wBAAgB,OAAO,CAAC,MAAM,EAAE,SAAS,EAAE,YAAY,EAAE,MAAM,EAAE,YAAY,EAAE,MAAM,GAAG,MAAM,CAc7F;AAED;;;;;;GAMG;AACH,wBAAgB,MAAM,CAAC,MAAM,EAAE,SAAS,EAAE,CAAC,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C,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,UAAU,CAGlE;AAED;;;GAGG;AACH,wBAAgB,YAAY,CAAC,UAAU,EAAE,UAAU,EAAE,IAAI,EAAE,MAAM,GAAG,SAAS,CAO5E;AAED;;;GAGG;AACH,wBAAgB,kBAAkB,CAAC,UAAU,EAAE,UAAU,GAAG,kBAAkB,CAW7E;AAED;;;;GAIG;AACH,wBAAgB,iBAAiB,CAAC,QAAQ,EAAE,kBAAkB,GAAG,UAAU,CAiB1E"}
package/dist/esm/rng.js CHANGED
@@ -99,6 +99,33 @@ export function chance(stream, p) {
99
99
  }
100
100
  return nextFloat(stream) < p;
101
101
  }
102
+ /**
103
+ * Pick one element of `items` uniformly. Valid calls consume exactly one
104
+ * `nextInt` draw; an empty array is rejected without consuming the stream,
105
+ * so a caller can never silently skip a draw it meant to make.
106
+ */
107
+ export function pick(stream, items) {
108
+ if (items.length === 0) {
109
+ throw new RangeError("pick: items must not be empty.");
110
+ }
111
+ return items[nextInt(stream, 0, items.length)];
112
+ }
113
+ /**
114
+ * Return a uniformly shuffled copy of `items` (Fisher–Yates, from the last
115
+ * index down). The input is never mutated. Valid calls consume exactly
116
+ * `items.length - 1` draws (none for zero or one element), so the draw count
117
+ * depends only on the length — replay stays aligned whatever the contents.
118
+ */
119
+ export function shuffle(stream, items) {
120
+ const out = [...items];
121
+ for (let i = out.length - 1; i > 0; i--) {
122
+ const j = nextInt(stream, 0, i + 1);
123
+ const swap = out[i];
124
+ out[i] = out[j];
125
+ out[j] = swap;
126
+ }
127
+ return out;
128
+ }
102
129
  /**
103
130
  * Capture `stream`'s internal seedrandom state as a plain, JSON-serializable
104
131
  * object (deep-cloned, so later draws on `stream` cannot mutate the
package/docs/API.md CHANGED
@@ -68,6 +68,8 @@ function nextU32(stream: RngStream): number;
68
68
  function nextFloat(stream: RngStream): number;
69
69
  function nextInt(stream: RngStream, minInclusive: number, maxExclusive: number): number;
70
70
  function chance(stream: RngStream, probability: number): boolean;
71
+ function pick<T>(stream: RngStream, items: readonly T[]): T;
72
+ function shuffle<T>(stream: RngStream, items: readonly T[]): T[];
71
73
  function snapshotStream(stream: RngStream): RngStreamSnapshot;
72
74
  function restoreStream(snapshot: RngStreamSnapshot): RngStream;
73
75
  function snapshotLayers(layers: RngLayers): RngLayersSnapshot;
@@ -79,6 +81,11 @@ function restoreLayers(snapshot: RngLayersSnapshot): RngLayers;
79
81
  - `nextInt` uses half-open bounds and exactly one float draw after validation.
80
82
  - `chance` consumes one draw for every valid probability, including `0` and
81
83
  `1`, keeping later draw positions predictable.
84
+ - `pick` returns one element uniformly with exactly one `nextInt` draw and
85
+ rejects an empty array without drawing.
86
+ - `shuffle` returns a Fisher–Yates shuffled copy (the input is never mutated)
87
+ and consumes exactly `length - 1` draws, so the draw count depends only on
88
+ the length, never on the contents.
82
89
  - Restore functions reject malformed ARC4 state and clone accepted state so a
83
90
  snapshot can seed multiple independent replays.
84
91
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "koota-kit",
3
- "version": "0.3.1",
3
+ "version": "0.4.0",
4
4
  "description": "Deterministic Koota simulation conventions: world lifecycle, dual RNG streams, safe object traits, and world-scoped event logs.",
5
5
  "type": "module",
6
6
  "license": "MIT",