Class: 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.


new RandomDataGenerator( [seeds])

Parameters:
Name Type Argument Description
seeds string | Array.<string> <optional>

The seeds to use for the random number generator.

Since: 3.0.0
Source: src/math/random-data-generator/RandomDataGenerator.js (Line 9)

Members


signs :Array.<number>

Signs to choose from.

Type:
  • Array.<number>
Since: 3.0.0
Source: src/math/random-data-generator/RandomDataGenerator.js (Line 92)

Methods


angle()

Returns a random angle between -180 and 180.

Since: 3.0.0
Source: src/math/random-data-generator/RandomDataGenerator.js (Line 412)
Returns:

A random number between -180 and 180.

Type
number

between(min, max)

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.

Since: 3.0.0
Source: src/math/random-data-generator/RandomDataGenerator.js (Line 278)
Returns:

A random number between min and max.

Type
number

frac()

Returns a random real number between 0 and 1.

Since: 3.0.0
Source: src/math/random-data-generator/RandomDataGenerator.js (Line 235)
Returns:

A random real number between 0 and 1.

Type
number

init(seeds)

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.

Since: 3.0.0
Source: src/math/random-data-generator/RandomDataGenerator.js (Line 163)

integer()

Returns a random integer between 0 and 2^32.

Since: 3.0.0
Source: src/math/random-data-generator/RandomDataGenerator.js (Line 221)
Returns:

A random integer between 0 and 2^32.

Type
number

integerInRange(min, max)

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.

Since: 3.0.0
Source: src/math/random-data-generator/RandomDataGenerator.js (Line 262)
Returns:

A random number between min and max.

Type
number

normal()

Returns a random real number between -1 and 1.

Since: 3.0.0
Source: src/math/random-data-generator/RandomDataGenerator.js (Line 311)
Returns:

A random real number between -1 and 1.

Type
number

pick(array)

Returns a random element from within the given array.

Parameters:
Name Type Description
array Array.<T>

The array to pick a random element from.

Since: 3.0.0
Source: src/math/random-data-generator/RandomDataGenerator.js (Line 345)
Returns:

A random member of the array.

Type
T

real()

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

Since: 3.0.0
Source: src/math/random-data-generator/RandomDataGenerator.js (Line 249)
Returns:

A random real number between 0 and 2^32.

Type
number

realInRange(min, max)

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.

Since: 3.0.0
Source: src/math/random-data-generator/RandomDataGenerator.js (Line 295)
Returns:

A random number between min and max.

Type
number

rotation()

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

Since: 3.0.0
Source: src/math/random-data-generator/RandomDataGenerator.js (Line 425)
Returns:

A random number between -3.141 and 3.141

Type
number

shuffle( [array])

Shuffles the given array, using the current seed.

Parameters:
Name Type Argument Description
array Array.<T> <optional>

The array to be shuffled.

Since: 3.7.0
Source: src/math/random-data-generator/RandomDataGenerator.js (Line 473)
Returns:

The shuffled array.

Type
Array.<T>

sign()

Returns a sign to be used with multiplication operator.

Since: 3.0.0
Source: src/math/random-data-generator/RandomDataGenerator.js (Line 364)
Returns:

-1 or +1.

Type
number

sow(seeds)

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.

Since: 3.0.0
Source: src/math/random-data-generator/RandomDataGenerator.js (Line 183)

state( [state])

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 Argument Description
state string <optional>

Generator state to be set.

Since: 3.0.0
Source: src/math/random-data-generator/RandomDataGenerator.js (Line 438)
Returns:

The current state of the generator.

Type
string

timestamp(min, max)

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.

Since: 3.0.0
Source: src/math/random-data-generator/RandomDataGenerator.js (Line 396)
Returns:

A random timestamp between min and max.

Type
number

uuid()

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

Since: 3.0.0
Source: src/math/random-data-generator/RandomDataGenerator.js (Line 324)
Returns:

A valid RFC4122 version4 ID hex string

Type
string

weightedPick(array)

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.

Since: 3.0.0
Source: src/math/random-data-generator/RandomDataGenerator.js (Line 377)
Returns:

A random member of the array.

Type
T