Random

Constructors

NameDescription
new(seed: number?)Creates a new pseudorandom number generator. If a seed is provided it is truncated to an integer within the range -9,007,199,254,740,991 to 9,007,199,254,740,991 and the generator becomes deterministic, always producing the same sequence for that seed. If no seed is given, one is drawn from an internal entropy source instead.

Code Snippet

local rng = Random.new(12345)

local roll = rng:NextInteger(1, 6)
local chance = rng:NextNumber()
local direction = rng:NextUnitVector()

print("Rolled a", roll, "with a roll chance of", chance, "facing", direction)

Methods

NameParametersReturnsDescription
NextInteger(min: number,max: number):numberMin: number, Max: numbernumberReturns a pseudorandom integer uniformly distributed over the range [min, max], inclusive of both endpoints.
NextNumber():numberVoidnumberReturns a uniform pseudorandom real number in the range of 0 to 1, inclusive.
NextNumber(min: number,max: number):numberMin: number, Max: numbernumberReturns a uniform pseudorandom real number in the range of min to max, inclusive.
NextUnitVector():Vector3VoidVector3Returns a unit vector with a pseudorandom direction. Vectors returned by this function are uniformly distributed over the unit sphere.
Shuffle(tb: table)Tb: tableVoidUniformly shuffles the array part of the given table in-place, using the Fisher-Yates algorithm. Throws an error if the table's array portion contains nil holes.
Clone():RandomVoidRandomReturns a new Random object whose internal state is copied from the original. After cloning, the original and the clone advance independently and will produce different results.