Quirrel4.41.0

math.srand

Defined in module "math"
from "math" import srand
fastcall srand(seed: int)

Sets the seed for rand()

Parameters

seedintthe new seed

Return value

Nothing; the call returns null.

Errors

Throws wrong number of parameters passed to native closure 'srand' (N passed, 2 required) when called with no argument, and a parameter type error when seed is not an integer (a float is not accepted).

Notes

seed is stored as an unsigned 32-bit value, so a negative seed wraps to the same value as seed + 2^32: srand(-1) and srand(4294967295) start the same sequence.

The seed lives in the shared state, one per VM rather than one per module, so this change is visible to rand() calls from every module.

Example

examples/math/srand.nut
from "math" import srand, rand

srand(5)
println("rand() =", rand())
println("rand() =", rand())

// re-seeding with the same value reproduces the same next draw
srand(5)
println("rand() =", rand())

// srand itself returns nothing useful
println("type(srand(5)) =", type(srand(5)))
Output:
rand() = 1022217549
rand() = 996754198
rand() = 1022217549
type(srand(5)) = null

See also

randReturns a random integer in range 0..RAND_MAX
mathmodule index