// reference

Math Libraries

This page documents the math libraries shipped under lib/math. These are library classes, not core built-in classes. Load them with intern.

math/Random

Random makes pseudo-random numbers. Made with a seed, it gives the same numbers, in the same order, on every run, so a simulation, a game or a training run can be repeated exactly. It is shipped as a Nex library under lib/math/random.nex, built on Java’s SplittableRandom. It is not meant for cryptography.

Loading

intern math/Random

Support

TargetSupported
JVM REPL / interpreterYes
Generated JVM codeYes

Construction

let repeatable: Random := create Random.with_seed(42)   -- same numbers every run
let different: Random := create Random.make             -- different numbers every run

Any Integer is a valid seed. Two generators made with the same seed give the same sequence, on both backends.

Methods

MethodArgumentsReturnsDescription
makenoneRandomA generator that starts somewhere different on every run.
with_seedseed: IntegerRandomA generator that gives the same numbers on every run with the same seed.
uniformnoneRealA Real from 0.0 up to, but not including, 1.0, every value equally likely.
belown: IntegerIntegerA whole number from 0 to n - 1, each equally likely. Requires n > 0.
betweenlow: Integer, high: IntegerIntegerA whole number from low to high, both included, each equally likely. Requires low <= high.
gaussiannoneRealA number from the standard normal distribution (the bell curve): mean 0, standard deviation 1. For mean m and spread s, use m + s * gaussian().

Functions

math/Random also brings in one generic function. (Nex methods cannot be generic, so it takes the generator as an argument.)

FunctionArgumentsReturnsDescription
shuffleitems: Array[T], random: RandomnonePut the elements of items in a random order, in place, every order equally likely (the Fisher–Yates shuffle).

Example

intern math/Random

let random: Random := create Random.with_seed(42)
print(random.between(1, 6))               -- a die roll: 1 with this seed
print(random.uniform() < 0.25)            -- true a quarter of the time
let height: Real := 170.0 + 8.0 * random.gaussian()

let cards: Array[String] := ["A", "K", "Q", "J"]
shuffle(cards, random)
print(cards)                              -- ["K", "J", "Q", "A"] with this seed

For a quick random number without a generator, n.pick on an Integer gives a whole number from 0 to n - 1, but it cannot be seeded.