Random Numbers

NCL provides hardware random number generation and a deterministic pseudorandom number generator.

The available instructions are:

Instruction Type Source
RNG Integer Hardware RNG
F.RNG Floating-point Hardware RNG
PRNG Integer Deterministic PRNG
SEED — PRNG control

RNG and F.RNG generate random values independently of program state.

PRNG uses an internal 32-bit xorshift state to produce a deterministic sequence of integer values. The sequence can be controlled using SEED, allowing it to be reproduced when required.

The pseudorandom generator is integer-only. There is no floating-point F.PRNG.

Hardware RNG

The hardware random number generator provides both integer and floating-point random values.

It is independent of the pseudorandom generator and its seed.

RNG — Random Integer

RNG <dst> <max>

Generates a random integer between zero and the specified exclusive bound.

The bound extends away from zero:

max > 0   →   0 through max - 1
max < 0   →   max + 1 through 0
max = 0   →   0

Operands

Position Name Type Description
1 dst Location Destination location.
2 max Integer value Exclusive random bound.

Modified locations

Location Description
dst Random integer.

Examples

Generate a value from 0 through 9:

RNG $000 10
-- > $000 is between 0 and 9

Generate a value from -9 through 0:

RNG $000 -10
-- > $000 is between -9 and 0

A zero bound always produces zero:

RNG $000 0
-- > $000 = 0

Notes

max is exclusive.

A positive max generates values from 0 upward toward max.

A negative max generates values from 0 downward toward max.

A max of 0 always produces 0.

RNG is independent of the pseudorandom generator and its seed.

RNG is intended for general-purpose randomness and is not suitable for cryptographic use.

F.RNG — Random Floating-Point Value

F.RNG <dst> <max>

Generates a random floating-point value between zero and the specified bound.

The range extends from zero toward max.

For a positive bound:

0.0 ≤ result < max

For a negative bound:

max < result ≤ 0.0

A bound of zero produces 0.0.

Operands

Position Name Type Description
1 dst Location Destination location.
2 max Floating-point value Random bound.

Modified locations

Location Description
dst Random floating-point value.

Examples

Generate a value between zero and 1:

F.RNG $000 f1
-- > 0.0 <= $000 < 1.0

Generate a value toward a positive bound:

F.RNG $000 f10
-- > 0.0 <= $000 < 10.0

Generate a value toward a negative bound:

F.RNG $000 f-10
-- > -10.0 < $000 <= 0.0

A zero bound produces zero:

F.RNG $000 f0
-- > $000 = 0.000000

Notes

A positive max generates values from 0.0 upward toward max.

A negative max generates values from 0.0 downward toward max.

A max of 0.0 produces 0.0.

F.RNG uses the hardware random number generator and is independent of the pseudorandom generator and its seed.

There is no corresponding F.PRNG.

F.RNG is intended for general-purpose randomness and is not suitable for cryptographic use.

Pseudorandom RNG

The pseudorandom number generator maintains an internal 32-bit xorshift state.

Unlike ordinary program state, the PRNG state persists between program loads. Starting another program does not reset the generator.

Programs that simply require pseudorandom values may use the existing state. Programs requiring a specific or reproducible sequence should explicitly initialize the generator using SEED.

The pseudorandom generator produces integer values only.

PRNG — Pseudorandom Integer

PRNG <dst> <max>

Generates a pseudorandom integer between zero and the specified exclusive bound.

Like RNG, the bound extends away from zero:

max > 0   →   0 through max - 1
max < 0   →   max + 1 through 0
max = 0   →   0

Operands

Position Name Type Description
1 dst Location Destination location.
2 max Integer value Exclusive random bound.

Modified locations

Location Description
dst Pseudorandom integer.

Examples

Generate a value from 0 through 99:

PRNG $000 100
-- > $000 is between 0 and 99

Generate a value from -99 through 0:

PRNG $000 -100
-- > $000 is between -99 and 0

Generate a reproducible sequence:

SEED 12345
PRNG $000 100
PRNG $001 100

SEED 12345
PRNG $002 100
PRNG $003 100

-- > $000 = $002
-- > $001 = $003

Notes

max is exclusive.

A positive max generates values from 0 upward toward max.

A negative max generates values from 0 downward toward max.

A max of 0 always produces 0.

The generated sequence is deterministic for a given non-zero seed.

The PRNG state persists between program loads.

Programs should not rely on the PRNG having a particular initial state.

Programs requiring reproducible results should explicitly set a non-zero seed before using PRNG.

Calling PRNG advances the pseudorandom generator even when max is 0.

SEED — Pseudorandom Seed

SEED <seed>

Sets the internal state used by PRNG.

A non-zero seed directly initializes the pseudorandom generator.

A seed of 0 selects a random non-zero state using the hardware random number generator.

Operands

Position Name Type Description
1 seed Integer value Pseudorandom seed value.

Modified locations

This instruction does not modify any locations.

Examples

Start a repeatable pseudorandom sequence:

SEED 12345
PRNG $000 100
PRNG $001 100
-- > The same seed produces the same sequence each time

Restart that sequence:

SEED 12345
PRNG $000 100
PRNG $001 100

SEED 12345
PRNG $002 100
PRNG $003 100

-- > $000 = $002
-- > $001 = $003

Select a random seed:

SEED 0
-- > PRNG begins from a randomly selected non-zero state

Notes

Using the same non-zero seed reproduces the same pseudorandom sequence.

SEED 0 selects a random non-zero seed using the hardware RNG.

SEED affects only PRNG.

It has no effect on RNG or F.RNG.

The PRNG state is not automatically reset when another program is loaded.