Caffeine

Random

Reference Core Last updated: Lua 5 · Dryad 5

A deterministic generator: the same seed always gives the same sequence, on every platform.

All namespaces

Namespace: caffeine.random · Dryad prefix: caffeine_random_

setSeed#

Lua: caffeine.random.setSeed · Dryad: caffeine_random_set_seed

text
setSeed(seed)

Restarts the sequence.

ParametersTypeDescription
seednumberAny whole number; 0 is replaced by a fixed value.

Returns: none

Example

lua
caffeine.random.setSeed(1234)

value#

Lua: caffeine.random.value · Dryad: caffeine_random_value

text
value() -> number

Next random number.

Returns: number. A number in [0, 1).

Example

lua
local r = caffeine.random.value()

range#

Lua: caffeine.random.range · Dryad: caffeine_random_range

text
range(min, max) -> number

A random number in a range.

ParametersTypeDescription
minnumberLower bound (inclusive).
maxnumberUpper bound (exclusive).

Returns: number. A number in [min, max).

Example

lua
local x = caffeine.random.range(-5, 5)

int#

Lua: caffeine.random.int · Dryad: caffeine_random_int

text
int(min, max) -> number

A random whole number, both ends included.

ParametersTypeDescription
minnumberLower bound (inclusive).
maxnumberUpper bound (inclusive); reversed bounds are swapped.

Returns: number. A whole number in [min, max].

Example

lua
local roll = caffeine.random.int(1, 6)

chance#

Lua: caffeine.random.chance · Dryad: caffeine_random_chance

text
chance(p) -> boolean

A weighted coin flip.

ParametersTypeDescription
pnumberProbability from 0 to 1.

Returns: boolean. true with probability p.

Example

lua
if caffeine.random.chance(0.25) then end