Skip to main content
The Random class provides a seeded pseudo-random number generator you can use wherever you need unpredictable variety — spawning enemies, picking dialog lines, shuffling loot tables, or adding jitter to movement. Because each Random instance carries its own internal state, you can seed it with an integer or string to reproduce the same sequence across runs (great for procedural generation), or omit the seed to get a fresh random sequence every time. The state can also be serialised and deserialised, so you can save and restore a generator’s position in its sequence across sessions.

Constructors


Instance Methods

nextInteger

Returns a random integer. Without arguments the range is [1, max_integer]. With one argument the range is [1, maxInclusive]. With two arguments the range is [minInclusive, maxInclusive].

nextNumber

Returns a random floating-point number. Without arguments the range is [0, 1). With one argument the range is [0, maxExclusive). With two arguments the range is [minInclusive, maxExclusive).

nextGaussian

Returns a random number drawn from a standard Gaussian (normal) distribution with mean 0 and standard deviation 1. Useful for adding natural-feeling variation.

nextBoolean

Returns true or false with equal probability.

nextElement

Returns a random element from the given table (array portion).

nextIndex

Returns a random integer index into the given table (array portion).

shuffle

Shuffles the elements of the given table in place using a Fisher-Yates algorithm seeded by this generator.

serializeToTable

Captures the current generator state into a plain Lua table for persistence.

serialize

Captures the current generator state into a LocalObject for persistence.

Static Methods

randomSeed

Returns a fresh random integer that you can pass to Random.new to seed a new generator.

randomStringSeed

Returns a fresh random string suitable for use as a string seed.

deserializeFromTable

Reconstructs a Random instance from a table previously produced by serializeToTable.

deserialize

Reconstructs a Random instance from a LocalObject previously produced by serialize.

Usage Examples

Randomly enabling enemies at spawn

Random dialog choice

Shuffling a loot table

Reproducible generation with a fixed seed

Saving and restoring generator state