From 9800d977587820cb2fcc90e7a6dd2a1b9025b208 Mon Sep 17 00:00:00 2001 From: chrisklus Date: Fri, 21 Aug 2020 13:47:43 -0400 Subject: [PATCH] Verify number of calls to random for source and destination sim match, see https://github.com/phetsims/states-of-matter/issues/325 --- js/Random.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/js/Random.js b/js/Random.js index e1a68eb..a32d5d0 100644 --- a/js/Random.js +++ b/js/Random.js @@ -46,6 +46,19 @@ class Random { // Math.seedrandom is provided by seedrandom.js, see https://github.com/davidbau/seedrandom. // @private {function:number|null} initialized via setSeed below this.seedrandom = ( this.seed !== null ) ? new Math.seedrandom( this.seed + '' ) : null; + + // @public (read-only) - the number of times `nextDouble` is called + this.numberOfCalls = 0; + + Random.allRandomInstances.add( this ); + } + + /** + * Clears out this instance from all of the Random instances. + * @public + */ + dispose() { + Random.allRandomInstances.delete( this ); } /** @@ -135,6 +148,7 @@ class Random { * @returns {number} - the random number */ nextDouble() { + this.numberOfCalls++; return this.seed === null ? Math.random() : this.seedrandom(); // eslint-disable-line bad-sim-text } @@ -182,6 +196,8 @@ class Random { } } +Random.allRandomInstances = new Set(); + dot.register( 'Random', Random ); export default Random; \ No newline at end of file