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..100Choosing from a list of values with uneven weights is a common operation in Roguelike development. The getWeightedValue
method is useful for this task; just give it a JS object with numeric weight values (arbitrary numbers) and a corresponding key will be picked randomly.
RNG'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.
You can clone a RNG to obtain its copy. The copy is completely independent and pre-set to its parent's state. The copy can be further cloned.