Random¶
Overview¶
Random number generator with an optional seed. It uses seedrandom.js, a monkey patch for Math, see https://github.com/davidbau/seedrandom.
If you are developing a PhET Simulation, you should probably use the global DOT/dotRandom
because it provides built-in support for phet-io seeding and a check that it isn't used before the seed has been set.
@author John Blanco (PhET Interactive Simulations) @author Aaron Davis (PhET Interactive Simulations) @author Sam Reid (PhET Interactive Simulations) @author Mohamed Safi
Class Random¶
Constructor¶
new Random( providedOptions? : RandomOptions )¶
Instance Methods¶
dispose()¶
Clears out this instance from all the Random instances.
getSeed() : number | null¶
Gets the seed.
nextBoolean() : boolean¶
Returns the next pseudo-random boolean
nextInt( n : number ) : number¶
Returns the next pseudo random number from this random number generator sequence. The random number is an integer ranging from 0 to n-1. @returns an integer
nextIntBetween( min : number, max : number ) : number¶
Randomly select a random integer between min and max (inclusive). @param min - must be an integer @param max - must be an integer @returns an integer between min and max, inclusive
sample( array : T[] ) : T¶
Randomly select one element from the given array. @param array - from which one element will be selected, must have at least one element @returns the selected element from the array
shuffle( array : T[] ) : T[]¶
Creates an array of shuffled values, using a version of the Fisher-Yates shuffle. Adapted from lodash-2.4.1 by Sam Reid on Aug 16, 2016, See http://en.wikipedia.org/wiki/Fisher-Yates_shuffle. @param array - the array which will be shuffled @returns a new array with all the same elements in the passed-in array, in randomized order.
nextDouble() : number¶
Returns the next pseudo random number from this random number generator sequence in the range [0, 1) The distribution of the random numbers is uniformly distributed across the interval @returns the random number
nextDoubleBetween( min : number, max : number ) : number¶
Randomly selects a double in the range [min,max).
nextGaussian() : number¶
Returns the next gaussian-distributed random number from this random number generator sequence. The distribution of the random numbers is gaussian, with a mean = 0 and standard deviation = 1
nextDoubleInRange( range : Range ) : number¶
Gets the next random double in a Range. For min < max, the return value is [min,max), between min (inclusive) and max (exclusive). For min === max, the return value is min.
nextPointInBounds( bounds : Bounds2 ) : Vector2¶
Gets a random point within the provided Bounds2, [min,max)
setSeed( seed : number | null )¶
@param seed - if null, Math.random will be used to create the seed.
sampleProbabilities( weights : readonly number[] ) : number¶
Choose a numeric index from the array of weights. The array of weights does not need to be normalized. See https://stackoverflow.com/questions/8877249/generate-random-integers-with-probabilities See also ContinuousServer.weightedSampleTest which uses the same algorithm
Instance Properties¶
numberOfCalls¶
the number of times nextDouble
is called. Clients should not write to this value.