You’re reading the docs for Phaser v3.90.0. The latest is Phaser v4.1.0.

RandomDataGenerator

Phaser.Math.RandomDataGenerator

A seeded Random Data Generator.

Access via Phaser.Math.RND which is an instance of this class pre-defined

by Phaser. Or, create your own instance to use as you require.

The Math.RND generator is seeded by the Game Config property value seed.

If no such config property exists, a random number is used.

If you create your own instance of this class you should provide a seed for it.

If no seed is given it will use a 'random' one based on Date.now.

Constructor

new RandomDataGenerator([seeds])

Parameters

nametypeoptionaldescription
seedsstring | Array.<string>YesThe seeds to use for the random number generator.

Scope: static

Source: src/math/random-data-generator/RandomDataGenerator.js#L9
Since: 3.0.0

Public Members

signs

signs: Array.<number>

Description:

Signs to choose from.

Source: src/math/random-data-generator/RandomDataGenerator.js#L92
Since: 3.0.0


Public Methods

angle

instanceangle()

Description:

Returns a random angle between -180 and 180.

Returns: number - A random number between -180 and 180.

Source: src/math/random-data-generator/RandomDataGenerator.js#L412
Since: 3.0.0


between

instancebetween(min, max)

Description:

Returns a random integer between and including min and max.

This method is an alias for RandomDataGenerator.integerInRange.

Parameters:

nametypeoptionaldescription
minnumberNoThe minimum value in the range.
maxnumberNoThe maximum value in the range.

Returns: number - A random number between min and max.

Source: src/math/random-data-generator/RandomDataGenerator.js#L278
Since: 3.0.0


frac

instancefrac()

Description:

Returns a random real number between 0 and 1.

Returns: number - A random real number between 0 and 1.

Source: src/math/random-data-generator/RandomDataGenerator.js#L235
Since: 3.0.0


init

instanceinit(seeds)

Description:

Initialize the state of the random data generator.

Parameters:

nametypeoptionaldescription
seedsstring | Array.<string>NoThe seeds to initialize the random data generator with.

Source: src/math/random-data-generator/RandomDataGenerator.js#L163
Since: 3.0.0


integer

instanceinteger()

Description:

Returns a random integer between 0 and 2^32.

Returns: number - A random integer between 0 and 2^32.

Source: src/math/random-data-generator/RandomDataGenerator.js#L221
Since: 3.0.0


integerInRange

instanceintegerInRange(min, max)

Description:

Returns a random integer between and including min and max.

Parameters:

nametypeoptionaldescription
minnumberNoThe minimum value in the range.
maxnumberNoThe maximum value in the range.

Returns: number - A random number between min and max.

Source: src/math/random-data-generator/RandomDataGenerator.js#L262
Since: 3.0.0


normal

instancenormal()

Description:

Returns a random real number between -1 and 1.

Returns: number - A random real number between -1 and 1.

Source: src/math/random-data-generator/RandomDataGenerator.js#L311
Since: 3.0.0


pick

instancepick(array)

Description:

Returns a random element from within the given array.

Tags:

  • generic
  • genericUse
  • genericUse

Parameters:

nametypeoptionaldescription
arrayArray.<T>NoThe array to pick a random element from.

Returns: T - A random member of the array.

Source: src/math/random-data-generator/RandomDataGenerator.js#L345
Since: 3.0.0


real

instancereal()

Description:

Returns a random real number between 0 and 2^32.

Returns: number - A random real number between 0 and 2^32.

Source: src/math/random-data-generator/RandomDataGenerator.js#L249
Since: 3.0.0


realInRange

instancerealInRange(min, max)

Description:

Returns a random real number between min and max.

Parameters:

nametypeoptionaldescription
minnumberNoThe minimum value in the range.
maxnumberNoThe maximum value in the range.

Returns: number - A random number between min and max.

Source: src/math/random-data-generator/RandomDataGenerator.js#L295
Since: 3.0.0


rotation

instancerotation()

Description:

Returns a random rotation in radians, between -3.141 and 3.141

Returns: number - A random number between -3.141 and 3.141

Source: src/math/random-data-generator/RandomDataGenerator.js#L425
Since: 3.0.0


shuffle

instanceshuffle([array])

Description:

Shuffles the given array, using the current seed.

Tags:

  • generic
  • genericUse

Parameters:

nametypeoptionaldescription
arrayArray.<T>YesThe array to be shuffled.

Returns: Array.<T> - The shuffled array.

Source: src/math/random-data-generator/RandomDataGenerator.js#L473
Since: 3.7.0


sign

instancesign()

Description:

Returns a sign to be used with multiplication operator.

Returns: number - -1 or +1.

Source: src/math/random-data-generator/RandomDataGenerator.js#L364
Since: 3.0.0


sow

instancesow(seeds)

Description:

Reset the seed of the random data generator.

Note: the seed array is only processed up to the first undefined (or null) value, should such be present.

Parameters:

nametypeoptionaldescription
seedsArray.<string>NoThe array of seeds: the toString() of each value is used.

Source: src/math/random-data-generator/RandomDataGenerator.js#L183
Since: 3.0.0


state

instancestate([state])

Description:

Gets or Sets the state of the generator. This allows you to retain the values

that the generator is using between games, i.e. in a game save file.

To seed this generator with a previously saved state you can pass it as the

seed value in your game config, or call this method directly after Phaser has booted.

Call this method with no parameters to return the current state.

If providing a state it should match the same format that this method

returns, which is a string with a header !rnd followed by the c,

s0, s1 and s2 values respectively, each comma-delimited.

Parameters:

nametypeoptionaldescription
statestringYesGenerator state to be set.

Returns: string - The current state of the generator.

Source: src/math/random-data-generator/RandomDataGenerator.js#L438
Since: 3.0.0


timestamp

instancetimestamp(min, max)

Description:

Returns a random timestamp between min and max, or between the beginning of 2000 and the end of 2020 if min and max aren't specified.

Parameters:

nametypeoptionaldescription
minnumberNoThe minimum value in the range.
maxnumberNoThe maximum value in the range.

Returns: number - A random timestamp between min and max.

Source: src/math/random-data-generator/RandomDataGenerator.js#L396
Since: 3.0.0


uuid

instanceuuid()

Description:

Returns a valid RFC4122 version4 ID hex string from https://gist.github.com/1308368

Returns: string - A valid RFC4122 version4 ID hex string

Source: src/math/random-data-generator/RandomDataGenerator.js#L324
Since: 3.0.0


weightedPick

instanceweightedPick(array)

Description:

Returns a random element from within the given array, favoring the earlier entries.

Tags:

  • generic
  • genericUse
  • genericUse

Parameters:

nametypeoptionaldescription
arrayArray.<T>NoThe array to pick a random element from.

Returns: T - A random member of the array.

Source: src/math/random-data-generator/RandomDataGenerator.js#L377
Since: 3.0.0