MotionScript

@motion-script/core


@motion-script/core / Random

Class: Random

Defined in: util/random.ts:58

A seeded random source handed to scene generators by stage.random(seed).

Wraps a SeedGenerator (mulberry32) and exposes the author-facing draw surface. Because the seed lives on the Random instance, determinism is scoped per source: two scenes can each stage.random("name") and get their own reproducible stream without touching a shared stage-level seed.

const random = stage.random("sparkle"); const xs = random.floatArray(40, -100, 100); // 40 reproducible offsets const drift = random.noise(t, 6); // smooth field over time

Two families live here, and they are NOT interchangeable:

  • Independent drawsnextFloat/nextInt/gauss and their array forms. Successive calls are uncorrelated; gauss only changes the distribution (bell curve vs flat), not the independence.
  • Correlated fieldnoise(t). Nearby inputs return nearby values, so it is the right tool for smooth motion over time/space (drift, shake, waves). No distribution draw can reproduce that smoothness.

Constructors

Constructor

new Random(seed): Random

Defined in: util/random.ts:73

Parameters

seed

string | number

Returns

Random

Accessors

seed

Get Signature

get seed(): string | number

Defined in: util/random.ts:81

The seed this source rewinds to. Assigning a new value re-anchors the source (same effect as reset(seed)): the next draws reproduce the sequence for that seed, and noise's lattice shifts to match.

Returns

string | number

Set Signature

set seed(seed): void

Defined in: util/random.ts:84

Parameters
seed

string | number

Returns

void

Methods

floatArray()

floatArray(size, from?, to?): number[]

Defined in: util/random.ts:124

size independent floats in [from, to).

Parameters

size

number

from?

number = 0

to?

number = 1

Returns

number[]


gauss()

gauss(mean?, stdev?): number

Defined in: util/random.ts:146

A normally-distributed sample with the given mean and stdev (default standard normal). Values cluster near the mean and tail off — useful for natural-looking scatter where most draws stay central with occasional outliers.

Uses the polar Box–Muller transform, which yields two independent normals per pair of uniforms; the spare is cached and returned on the next call.

Parameters

mean?

number = 0

stdev?

number = 1

Returns

number


intArray()

intArray(size, from?, to?): number[]

Defined in: util/random.ts:131

size independent integers in [from, to).

Parameters

size

number

from?

number = 0

to?

number = UINT32_RANGE

Returns

number[]


nextFloat()

nextFloat(from?, to?): number

Defined in: util/random.ts:111

The next pseudo-random float in [from, to). Defaults to [0, 1).

Parameters

from?

number = 0

to?

number = 1

Returns

number


nextInt()

nextInt(from?, to?): number

Defined in: util/random.ts:119

The next pseudo-random integer in [from, to). Defaults to the full unsigned 32-bit range [0, 2^32).

Parameters

from?

number = 0

to?

number = UINT32_RANGE

Returns

number


noise()

noise(time, frequency?): number

Defined in: util/random.ts:178

Smooth 1-D value noise in [0, 1].

Samples two adjacent lattice points around time * frequency, then interpolates with a smoothstep curve (3t² - 2t³) to avoid the linear kinks of plain lerp. Unlike nextFloat/gauss, nearby time inputs return nearby values — this is the correlated field used for organic, continuously varying motion (camera shake, drift, waves).

Parameters

time

number

Sample position (e.g. normalized timeline [0, 1], or a spatial index).

frequency?

number = 1

Scales time before sampling; higher = faster oscillation / more crests across the same span.

Returns

number


reset()

reset(seed?): void

Defined in: util/random.ts:198

Rewind this source so the next draws reproduce a sequence from the start.

With no argument, rewinds to the current seed — what authors call to repeat a sequence within a scene. Pass a seed to adopt it as the new origin first (equivalent to assigning seed), so the source both re-seeds and rewinds in one call — e.g. in a node's constructor to give it a distinct reproducible stream.

Parameters

seed?

string | number

Returns

void