@m2c2kit/core 0.3.32 → 0.3.34

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/dist/index.d.ts CHANGED
@@ -4867,6 +4867,44 @@ interface PluginEvent extends M2Event<Plugin> {
4867
4867
  }
4868
4868
 
4869
4869
  declare class RandomDraws {
4870
+ private static randomFunction;
4871
+ private static seededPRNG;
4872
+ /**
4873
+ * Sets the seed for the pseudo-random number generator (PRNG) and
4874
+ * instructs methods within `RandomDraws` to use a seeded PRNG
4875
+ * instead of the default `Math.random()`.
4876
+ *
4877
+ * @remarks The implementation of the seeded PRNG is based on David Bau's
4878
+ * `seedrandom` library at https://github.com/davidbau/seedrandom
4879
+ *
4880
+ * @param seed - The seed string to initialize the PRNG.
4881
+ */
4882
+ static setSeed(seed: string): void;
4883
+ /**
4884
+ * Instructs methods within `RandomDraws` to use the default
4885
+ * `Math.random()` from the runtime environment as the random number
4886
+ * function instead of a seeded PRNG.
4887
+ */
4888
+ static useDefaultRandom(): void;
4889
+ /**
4890
+ * Instructs methods within `RandomDraws` to use the seeded
4891
+ * pseudo-random number generator (PRNG).
4892
+ *
4893
+ * @remarks This method will throw an error if `setSeed()` has not
4894
+ * been called first to initialize the seeded PRNG.
4895
+ */
4896
+ static useSeededRandom(): void;
4897
+ /**
4898
+ * Generates a random number in the range [0, 1) using the current
4899
+ * random function.
4900
+ *
4901
+ * @remarks This method will return a number from `Math.random()` in the
4902
+ * runtime environment, unless `setSeed()` has been called to initialize
4903
+ * a seeded PRNG.
4904
+ *
4905
+ * @returns A random number in the range [0, 1) using the current random function.
4906
+ */
4907
+ static random(): number;
4870
4908
  /**
4871
4909
  * Draws a single random integer from a uniform distribution of integers in
4872
4910
  * the specified range.
@@ -4875,6 +4913,10 @@ declare class RandomDraws {
4875
4913
  * @param maximumInclusive - Upper bound of range
4876
4914
  * @returns A sampled integer
4877
4915
  */
4916
+ static singleFromRange(minimumInclusive: number, maximumInclusive: number): number;
4917
+ /**
4918
+ * @deprecated Use `singleFromRange()` instead.
4919
+ */
4878
4920
  static SingleFromRange(minimumInclusive: number, maximumInclusive: number): number;
4879
4921
  /**
4880
4922
  * Draws random integers, without replacement, from a uniform distribution
@@ -4885,6 +4927,10 @@ declare class RandomDraws {
4885
4927
  * @param maximumInclusive - Upper bound of range
4886
4928
  * @returns An array of integers
4887
4929
  */
4930
+ static fromRangeWithoutReplacement(n: number, minimumInclusive: number, maximumInclusive: number): Array<number>;
4931
+ /**
4932
+ * @deprecated Use `fromRangeWithoutReplacement()` instead.
4933
+ */
4888
4934
  static FromRangeWithoutReplacement(n: number, minimumInclusive: number, maximumInclusive: number): Array<number>;
4889
4935
  /**
4890
4936
  * Draw random grid cell locations, without replacement, from a uniform
@@ -4902,6 +4948,13 @@ declare class RandomDraws {
4902
4948
  * @returns Array of grid cells. Each cell is object in form of:
4903
4949
  * &#123 row: number, column: number &#125;. Grid cell locations are zero-based
4904
4950
  */
4951
+ static fromGridWithoutReplacement(n: number, rows: number, columns: number, predicate?: (row: number, column: number) => boolean): Array<{
4952
+ row: number;
4953
+ column: number;
4954
+ }>;
4955
+ /**
4956
+ * @deprecated Use `fromGridWithoutReplacement()` instead.
4957
+ */
4905
4958
  static FromGridWithoutReplacement(n: number, rows: number, columns: number, predicate?: (row: number, column: number) => boolean): Array<{
4906
4959
  row: number;
4907
4960
  column: number;
package/dist/index.js CHANGED
@@ -12762,7 +12762,57 @@ const _LegacyTimer = class _LegacyTimer {
12762
12762
  _LegacyTimer._timers = new Array();
12763
12763
  let LegacyTimer = _LegacyTimer;
12764
12764
 
12765
- class RandomDraws {
12765
+ const _RandomDraws = class _RandomDraws {
12766
+ /**
12767
+ * Sets the seed for the pseudo-random number generator (PRNG) and
12768
+ * instructs methods within `RandomDraws` to use a seeded PRNG
12769
+ * instead of the default `Math.random()`.
12770
+ *
12771
+ * @remarks The implementation of the seeded PRNG is based on David Bau's
12772
+ * `seedrandom` library at https://github.com/davidbau/seedrandom
12773
+ *
12774
+ * @param seed - The seed string to initialize the PRNG.
12775
+ */
12776
+ static setSeed(seed) {
12777
+ this.seededPRNG = seedrandom(seed);
12778
+ this.randomFunction = this.seededPRNG;
12779
+ }
12780
+ /**
12781
+ * Instructs methods within `RandomDraws` to use the default
12782
+ * `Math.random()` from the runtime environment as the random number
12783
+ * function instead of a seeded PRNG.
12784
+ */
12785
+ static useDefaultRandom() {
12786
+ this.randomFunction = Math.random;
12787
+ }
12788
+ /**
12789
+ * Instructs methods within `RandomDraws` to use the seeded
12790
+ * pseudo-random number generator (PRNG).
12791
+ *
12792
+ * @remarks This method will throw an error if `setSeed()` has not
12793
+ * been called first to initialize the seeded PRNG.
12794
+ */
12795
+ static useSeededRandom() {
12796
+ if (this.seededPRNG === null) {
12797
+ throw new M2Error(
12798
+ "Cannot use seeded random function because no seed has been set. Call setSeed() first."
12799
+ );
12800
+ }
12801
+ this.randomFunction = this.seededPRNG;
12802
+ }
12803
+ /**
12804
+ * Generates a random number in the range [0, 1) using the current
12805
+ * random function.
12806
+ *
12807
+ * @remarks This method will return a number from `Math.random()` in the
12808
+ * runtime environment, unless `setSeed()` has been called to initialize
12809
+ * a seeded PRNG.
12810
+ *
12811
+ * @returns A random number in the range [0, 1) using the current random function.
12812
+ */
12813
+ static random() {
12814
+ return this.randomFunction();
12815
+ }
12766
12816
  /**
12767
12817
  * Draws a single random integer from a uniform distribution of integers in
12768
12818
  * the specified range.
@@ -12771,10 +12821,26 @@ class RandomDraws {
12771
12821
  * @param maximumInclusive - Upper bound of range
12772
12822
  * @returns A sampled integer
12773
12823
  */
12774
- static SingleFromRange(minimumInclusive, maximumInclusive) {
12775
- const sampledNumber = Math.floor(Math.random() * (maximumInclusive - minimumInclusive + 1)) + minimumInclusive;
12824
+ static singleFromRange(minimumInclusive, maximumInclusive) {
12825
+ if (!Number.isInteger(minimumInclusive) || !Number.isInteger(maximumInclusive)) {
12826
+ throw new M2Error("All inputs must be integers");
12827
+ }
12828
+ if (maximumInclusive < minimumInclusive) {
12829
+ throw new M2Error(
12830
+ `maximumInclusive (${maximumInclusive}) must be >= minimumInclusive (${minimumInclusive})`
12831
+ );
12832
+ }
12833
+ const sampledNumber = Math.floor(
12834
+ this.randomFunction() * (maximumInclusive - minimumInclusive + 1)
12835
+ ) + minimumInclusive;
12776
12836
  return sampledNumber;
12777
12837
  }
12838
+ /**
12839
+ * @deprecated Use `singleFromRange()` instead.
12840
+ */
12841
+ static SingleFromRange(minimumInclusive, maximumInclusive) {
12842
+ return this.singleFromRange(minimumInclusive, maximumInclusive);
12843
+ }
12778
12844
  /**
12779
12845
  * Draws random integers, without replacement, from a uniform distribution
12780
12846
  * of integers in the specified range.
@@ -12784,21 +12850,39 @@ class RandomDraws {
12784
12850
  * @param maximumInclusive - Upper bound of range
12785
12851
  * @returns An array of integers
12786
12852
  */
12787
- static FromRangeWithoutReplacement(n, minimumInclusive, maximumInclusive) {
12788
- if (n > maximumInclusive - minimumInclusive + 1) {
12853
+ static fromRangeWithoutReplacement(n, minimumInclusive, maximumInclusive) {
12854
+ if (!Number.isInteger(n) || !Number.isInteger(minimumInclusive) || !Number.isInteger(maximumInclusive)) {
12855
+ throw new M2Error("All inputs must be integers");
12856
+ }
12857
+ const rangeSize = maximumInclusive - minimumInclusive + 1;
12858
+ if (n > rangeSize) {
12789
12859
  throw new M2Error(
12790
- `number of requested draws (n = ${n}) is greater than number of integers in range [ ${minimumInclusive}, ${maximumInclusive}]`
12860
+ `number of requested draws (n = ${n}) is greater than number of integers in range [${minimumInclusive}, ${maximumInclusive}]`
12791
12861
  );
12792
12862
  }
12793
- const result = new Array();
12794
- for (let i = 0; i < n; i++) {
12795
- const sampledNumber = RandomDraws.SingleFromRange(
12863
+ const selected = /* @__PURE__ */ new Set();
12864
+ for (let i = rangeSize - n; i < rangeSize; i++) {
12865
+ const t = _RandomDraws.singleFromRange(
12796
12866
  minimumInclusive,
12797
- maximumInclusive
12867
+ minimumInclusive + i
12798
12868
  );
12799
- result.includes(sampledNumber) ? n++ : result.push(sampledNumber);
12869
+ if (selected.has(t)) {
12870
+ selected.add(minimumInclusive + i);
12871
+ } else {
12872
+ selected.add(t);
12873
+ }
12800
12874
  }
12801
- return result;
12875
+ return Array.from(selected);
12876
+ }
12877
+ /**
12878
+ * @deprecated Use `fromRangeWithoutReplacement()` instead.
12879
+ */
12880
+ static FromRangeWithoutReplacement(n, minimumInclusive, maximumInclusive) {
12881
+ return this.fromRangeWithoutReplacement(
12882
+ n,
12883
+ minimumInclusive,
12884
+ maximumInclusive
12885
+ );
12802
12886
  }
12803
12887
  /**
12804
12888
  * Draw random grid cell locations, without replacement, from a uniform
@@ -12816,10 +12900,10 @@ class RandomDraws {
12816
12900
  * @returns Array of grid cells. Each cell is object in form of:
12817
12901
  * &#123 row: number, column: number &#125;. Grid cell locations are zero-based
12818
12902
  */
12819
- static FromGridWithoutReplacement(n, rows, columns, predicate) {
12903
+ static fromGridWithoutReplacement(n, rows, columns, predicate) {
12820
12904
  const result = new Array();
12821
12905
  const maximumInclusive = rows * columns - 1;
12822
- const draws = this.FromRangeWithoutReplacement(n, 0, maximumInclusive);
12906
+ const draws = this.fromRangeWithoutReplacement(n, 0, maximumInclusive);
12823
12907
  let i = 0;
12824
12908
  let replacementCell = NaN;
12825
12909
  while (i < n) {
@@ -12830,7 +12914,7 @@ class RandomDraws {
12830
12914
  i++;
12831
12915
  } else {
12832
12916
  do {
12833
- replacementCell = this.FromRangeWithoutReplacement(
12917
+ replacementCell = this.fromRangeWithoutReplacement(
12834
12918
  1,
12835
12919
  0,
12836
12920
  maximumInclusive
@@ -12841,6 +12925,98 @@ class RandomDraws {
12841
12925
  }
12842
12926
  return result;
12843
12927
  }
12928
+ /**
12929
+ * @deprecated Use `fromGridWithoutReplacement()` instead.
12930
+ */
12931
+ static FromGridWithoutReplacement(n, rows, columns, predicate) {
12932
+ return this.fromGridWithoutReplacement(n, rows, columns, predicate);
12933
+ }
12934
+ };
12935
+ _RandomDraws.randomFunction = Math.random;
12936
+ _RandomDraws.seededPRNG = null;
12937
+ let RandomDraws = _RandomDraws;
12938
+ const pool = [];
12939
+ const width = 256;
12940
+ const chunks = 6;
12941
+ const digits = 52;
12942
+ const startDenom = Math.pow(width, chunks);
12943
+ const significance = Math.pow(2, digits);
12944
+ const overflow = significance * 2;
12945
+ const mask = width - 1;
12946
+ function seedrandom(seed) {
12947
+ const key = [];
12948
+ mixKey(seed, key);
12949
+ const arc4 = new ARC4(key);
12950
+ const prng = (() => {
12951
+ let n = arc4.g(chunks);
12952
+ let d = startDenom;
12953
+ let x = 0;
12954
+ while (n < significance) {
12955
+ n = (n + x) * width;
12956
+ d *= width;
12957
+ x = arc4.g(1);
12958
+ }
12959
+ while (n >= overflow) {
12960
+ n /= 2;
12961
+ d /= 2;
12962
+ x >>>= 1;
12963
+ }
12964
+ return (n + x) / d;
12965
+ });
12966
+ prng.int32 = () => arc4.g(4) | 0;
12967
+ prng.quick = () => arc4.g(4) / 4294967296;
12968
+ prng.double = prng;
12969
+ mixKey(tostring(arc4.S), pool);
12970
+ return prng;
12971
+ }
12972
+ class ARC4 {
12973
+ constructor(key) {
12974
+ this.i = 0;
12975
+ this.j = 0;
12976
+ this.S = [];
12977
+ let t;
12978
+ let keylen = key.length;
12979
+ const s = this.S;
12980
+ if (!keylen) key = [keylen++];
12981
+ for (let i = 0; i < width; i++) s[i] = i;
12982
+ for (let i = 0, j = 0; i < width; i++) {
12983
+ t = s[i];
12984
+ j = mask & j + key[i % keylen] + t;
12985
+ s[i] = s[j];
12986
+ s[j] = t;
12987
+ }
12988
+ this.g(width);
12989
+ }
12990
+ /** Returns the next `count` outputs concatenated as a single number. */
12991
+ g(count) {
12992
+ let r = 0;
12993
+ let i = this.i;
12994
+ let j = this.j;
12995
+ const s = this.S;
12996
+ while (count--) {
12997
+ const t = s[i = mask & i + 1];
12998
+ const u = s[j = mask & j + t];
12999
+ s[i] = u;
13000
+ s[j] = t;
13001
+ r = r * width + s[mask & u + t];
13002
+ }
13003
+ this.i = i;
13004
+ this.j = j;
13005
+ return r;
13006
+ }
13007
+ }
13008
+ function mixKey(seed, key) {
13009
+ let hash = 0;
13010
+ let j = 0;
13011
+ while (j < seed.length) {
13012
+ key[mask & j] = mask & (hash ^= key[mask & j] * 19) + seed.charCodeAt(j);
13013
+ j++;
13014
+ }
13015
+ return tostring(key);
13016
+ }
13017
+ function tostring(a) {
13018
+ const arr = Array.prototype.slice.call(a, 0);
13019
+ return String.fromCharCode.apply(null, arr);
12844
13020
  }
12845
13021
 
12846
13022
  class SoundPlayer extends M2Node {
@@ -13300,7 +13476,7 @@ class Story {
13300
13476
  }
13301
13477
  }
13302
13478
 
13303
- console.log("\u26AA @m2c2kit/core version 0.3.32 (f8bdff76)");
13479
+ console.log("\u26AA @m2c2kit/core version 0.3.34 (d289d12c)");
13304
13480
 
13305
13481
  export { Action, ActivityType, CanvasKitHelpers, ColorfulMutablePath, Composite, Constants, ConstraintType, CustomAction, Dimensions, Easings, Equal, Equals, EventStore, EventStoreMode, FadeAlphaAction, FontManager, Game, GroupAction, I18n, ImageManager, Label, LabelHorizontalAlignmentMode, LayoutConstraint, LegacyTimer, M2Error, M2EventType, M2ImageStatus, M2Node, M2NodeFactory, M2NodeType, M2SoundStatus, M2c2KitHelpers, MoveAction, MutablePath, NoneTransition, PlayAction, RandomDraws, RepeatAction, RepeatForeverAction, RotateAction, ScaleAction, Scene, SceneTransition, SequenceAction, Shape, ShapeType, SlideTransition, SoundManager, SoundPlayer, SoundRecorder, Sprite, Story, TextLine, Timer, Transition, TransitionDirection, TransitionType, Uuid, WaitAction, WebColors, WebGlInfo, handleInterfaceOptions };
13306
13482
  //# sourceMappingURL=index.js.map