Phaser API Documentation

  Version: 
Filter
This is a beta release of our new docs system. Found an issue?
Please tell us about it in the #📖-newdocs-feedback channel on the Phaser Discord

Phaser . Math . RandomDataGenerator

This is a beta release of our new docs system. Found an issue?
Please tell us about it in the #📖-newdocs-feedback channel on the Phaser Discord

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:

name type arguments description
seeds string | Array.<string> <optional>

The seeds to use for the random number generator.

Members

<private> c: number
Focus
Focus

Description:

Internal var.

Type:
number
Default: 1
Focus
Focus
<private> n: number
Focus
Focus

Description:

Internal var.

Type:
number
Default: 0
Focus
Focus
<private> s0: number
Focus
Focus

Description:

Internal var.

Type:
number
Default: 0
Focus
Focus
<private> s1: number
Focus
Focus

Description:

Internal var.

Type:
number
Default: 0
Focus
Focus
<private> s2: number
Focus
Focus

Description:

Internal var.

Type:
number
Default: 0
Focus
Focus
signs: Array.<number>
Focus
Focus

Description:

Signs to choose from.

Type:
Array.<number>
Focus
Focus

Methods

angle()
Focus
Focus

Description:

Returns a random angle between -180 and 180.

Returns:
Description:

A random number between -180 and 180.

Type:
  • number
Examples:
Focus
Focus
between(min, max)
Focus
Focus

Description:

Returns a random integer between and including min and max. This method is an alias for RandomDataGenerator.integerInRange.

Parameters:

name type description
min number

The minimum value in the range.

max number

The maximum value in the range.

Returns:
Description:

A random number between min and max.

Type:
  • number
Examples:
Focus
Focus
frac()
Focus
Focus

Description:

Returns a random real number between 0 and 1.

Returns:
Description:

A random real number between 0 and 1.

Type:
  • number
Examples:
Focus
Focus
<private> hash(data)
Focus
Focus

Description:

Internal method that creates a seed hash.

Parameters:

name type description
data string

The value to hash.

Returns:
Description:

The hashed value.

Type:
  • number
Examples:
Focus
Focus
init(seeds)
Focus
Focus

Description:

Initialize the state of the random data generator.

Parameters:

name type description
seeds string | Array.<string>

The seeds to initialize the random data generator with.

Examples:
Focus
Focus
integer()
Focus
Focus

Description:

Returns a random integer between 0 and 2^32.

Returns:
Description:

A random integer between 0 and 2^32.

Type:
  • number
Examples:
Focus
Focus
integerInRange(min, max)
Focus
Focus

Description:

Returns a random integer between and including min and max.

Parameters:

name type description
min number

The minimum value in the range.

max number

The maximum value in the range.

Returns:
Description:

A random number between min and max.

Type:
  • number
Examples:
Focus
Focus
normal()
Focus
Focus

Description:

Returns a random real number between -1 and 1.

Returns:
Description:

A random real number between -1 and 1.

Type:
  • number
Examples:
Focus
Focus
pick(array)
Focus
Focus

Description:

Returns a random element from within the given array.

Parameters:

name type description
array Array.<T>

The array to pick a random element from.

Returns:
Description:

A random member of the array.

Type:
  • T
Examples:
Focus
Focus
real()
Focus
Focus

Description:

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

Returns:
Description:

A random real number between 0 and 2^32.

Type:
  • number
Examples:
Focus
Focus
realInRange(min, max)
Focus
Focus

Description:

Returns a random real number between min and max.

Parameters:

name type description
min number

The minimum value in the range.

max number

The maximum value in the range.

Returns:
Description:

A random number between min and max.

Type:
  • number
Examples:
Focus
Focus
<private> rnd()
Focus
Focus

Description:

Private random helper.

Returns:
Description:

A random number.

Type:
  • number
Examples:
Focus
Focus
rotation()
Focus
Focus

Description:

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

Returns:
Description:

A random number between -3.141 and 3.141

Type:
  • number
Examples:
Focus
Focus
shuffle([array])
Focus
Focus

Description:

Shuffles the given array, using the current seed.

Parameters:

name type arguments description
array Array.<T> <optional>

The array to be shuffled.

Returns:
Description:

The shuffled array.

Type:
  • Array.
Examples:
Focus
Focus
sign()
Focus
Focus

Description:

Returns a sign to be used with multiplication operator.

Returns:
Description:

-1 or +1.

Type:
  • number
Examples:
Focus
Focus
sow(seeds)
Focus
Focus

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:

name type description
seeds Array.<string>

The array of seeds: the toString() of each value is used.

Examples:
Focus
Focus
state([state])
Focus
Focus

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:

name type arguments description
state string <optional>

Generator state to be set.

Returns:
Description:

The current state of the generator.

Type:
  • string
Examples:
Focus
Focus
timestamp(min, max)
Focus
Focus

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:

name type description
min number

The minimum value in the range.

max number

The maximum value in the range.

Returns:
Description:

A random timestamp between min and max.

Type:
  • number
Examples:
Focus
Focus
uuid()
Focus
Focus

Description:

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

Returns:
Description:

A valid RFC4122 version4 ID hex string

Type:
  • string
Examples:
Focus
Focus
weightedPick(array)
Focus
Focus

Description:

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

Parameters:

name type description
array Array.<T>

The array to pick a random element from.

Returns:
Description:

A random member of the array.

Type:
  • T
Examples:
Focus
Focus