util.math.randomseed

Shared

Sets the seed for the pseudo-random number generator


Syntax

util.math.randomseed(
    seed,
    seed_extra = nil
)

Parameters

TypeNameDescription
intseedPrimary seed value
intseed_extraOptional secondary seed value for a wider seed range

Returns

This function does not return any values.


Examples

Seed with a fixed value for reproducibility
util.math.randomseed(42)

core.engine.print("info", util.math.random(100)) -- always the same value
Seed with the current time for unique sequences
util.math.randomseed(os.time())

core.engine.print("info", util.math.random(100)) -- varies per run
Two seeds with the same value produce the same sequence
util.math.randomseed(1234)
local a = util.math.random(1000)

util.math.randomseed(1234)
local b = util.math.random(1000)

core.engine.print("info", a == b) -- true
Seed before generating a reproducible shuffle
util.math.randomseed(7)

local deck = {}

for i = 1, 10 do
    deck[i] = i
end

for i = #deck, 2, -1 do
    local j = util.math.random(i)
    deck[i], deck[j] = deck[j], deck[i]
end

core.engine.iprint(deck)
Use two seed components for a broader range
util.math.randomseed(os.time(), os.clock()*1e9)

core.engine.print("info", util.math.random()) -- 0.74392 (varies)

On this page