While the built-in Math.random()
function provides suitable results for game development purposes, it has its own weaknesses.
Most notably, it is not possible to seed the generator in order to reproduce a deterministic sequence of values. This is where ROT.RNG
object comes to play.
Note: We use the excellent Alea algorithm, developed by Johannes Baagøe. For more information about the code, please see his article on RNGs in JavaScript. Alea is distributed under the MIT License.
Three main modes of operation are available:
ROT.RNG.getUniform()
– random number [0..1) with uniform distribution (similar to Math.random()
)ROT.RNG.getNormal(mean, stddev)
– random number with normal distribution, parametrized by a mean value and standard deviationROT.RNG.getPercentage()
– random integer 1..100RNG's internal state can be retrieved and set to produce identical results.
The RNG can be seeded by a given number. Seeding initializes RNG's internal state. Retrieving the current seed is not very useful, but might come handy if you want to reproduce a behavior resulting from a random seed.