Utilities

Math and random

mathx and Random.

mathx

Numbers

FunctionResult
mathx.round(x, digits = 0)Rounded to that many decimal places
mathx.clamp(x, min, max)x limited to the range
mathx.sign(x)1, -1 or 0
mathx.isnan(x)true if x is not a number
mathx.rand(a, b)Random decimal between a and b
mathx.approach(value, target, step)Moves value toward target by at most step
mathx.smoothstep(t)Smooth curve from 0 to 1 (t is clamped)

Interpolation

FunctionResult
mathx.lerp(a, b, t)a + (b - a) * t
mathx.invlerp(a, b, v)Where v sits between a and b (0 to 1)
mathx.remap(v, a, b, c, d)Maps v from the range a..b to c..d

Statistics

FunctionResult
mathx.avg(t)Mean of an array
mathx.median(t)Median
mathx.stddev(t)Standard deviation

Angles

FunctionResult
mathx.wrap180(a)Angle wrapped to -180..180
mathx.angdiff(a, b)Shortest signed difference from a to b, in degrees

Distances

FunctionResult
mathx.dist2(x1, y1, x2, y2)2D distance
mathx.dist3(x1, y1, z1, x2, y2, z2)3D distance
mathx.len3(x, y, z)Length of a 3D vector
mathx.norm3(x, y, z)The vector scaled to length 1 (returns 0, 0, 0 for a zero vector)

Easing: mathx.ease

Functions that map t from 0 to 1 to a curve from 0 to 1. Available: linear, inQuad, outQuad, inOutQuad, inCubic, outCubic, inOutCubic, inSine, outSine, outBack, outBounce.

print(mathx.ease.outCubic(0.5))

Random

A seeded generator. The same seed always gives the same sequence.

Random.new(seed)

Creates a generator. Without a seed, it uses the current time.

rng:NextNumber(a, b)

With no arguments, a number from 0 up to 1. With a and b, a number between them.

rng:NextInteger(a, b)

A whole number from a to b, both included.

rng:Choice(array)

A random element.

rng:Shuffle(array)

Shuffles in place and returns the array.

local rng = Random.new(1234)
print(rng:NextInteger(1, 6))