Skip to content

Rand

Random numbers, either from Math.random() or from a seeded generator you can replay.

Both histograms come from one seeded generator via Rand.create(1337), so the shape is identical on every reload. float() is flat; distribution() averages five samples and converges toward the middle.

The module-level float(), integer(), distribution(), and pick() use Math.random() until you call seed(), after which they follow a deterministic mulberry32 sequence. Call seed() with no argument to hand them back to Math.random().

create() returns an independent generator with the same four methods, which is the better option when you want one reproducible stream without touching global state. distribution() averages several samples, biasing results toward the middle of the range instead of spreading them evenly.

js
import { Rand } from '@1pizzateam/spock';

const random = Rand.create(1337); // same seed, same level, every run

const flat = random.float(0, 1);
const enemies = random.integer(3, 8);
const centered = random.distribution(0, 1, 5); // clustered toward middle

Rand.float()

Uniform float in [min, max).

Uniform in [min, max): min can come up, max cannot.

ts
float(min: number, max: number): number

Parameters

  • minnumber.
  • maxnumber.

Returns

number — the result

Example

js
import { Rand } from '@1pizzateam/spock';


const result = Rand.float(1, 1);

Rand.integer()

Uniform integer in [min, max].

Uniform in [min, max], with both ends included, unlike float().

ts
integer(min: number, max: number): number

Parameters

  • minnumber.
  • maxnumber.

Returns

number — the result

Example

js
import { Rand } from '@1pizzateam/spock';


const result = Rand.integer(1, 1);

2D particle distribution

The same generator fills both panels in two dimensions. float() spreads points evenly across the square, while distribution() averages five samples per axis and pulls them into a soft cloud around the centre. pick() chooses each point's colour.
js
import { Rand } from '@1pizzateam/spock';

const random = Rand.create(20260904);

for (let i = 0; i < 9000; i++) {
  const x = random.distribution(0, width, 5);
  const y = random.distribution(0, height, 5);
  const tint = random.pick(0, 1);
}

Rand.distribution()

Average of iterations uniform samples in [min, max).

Averages iterations uniform samples. More iterations bunch results toward the middle of the range, approaching a bell shape, which is a quick way to make random placement look less evenly scattered.

ts
distribution(min: number, max: number, iterations: number): number

Parameters

  • minnumber.
  • maxnumber.
  • iterationsnumber.

Returns

number — the result

Example

js
import { Rand } from '@1pizzateam/spock';


const result = Rand.distribution(1, 1, 4);

Rand.pick()

Pick value1 or value2 with equal chance.

Returns one of the two values with even odds.

ts
pick(value1: number, value2: number): number

Parameters

  • value1number.
  • value2number.

Returns

number — the result

Example

js
import { Rand } from '@1pizzateam/spock';


const result = Rand.pick(1, 1);

Rand.seed()

Seed the default generator, or restore Math.random if omitted.

Switches the module-level functions to a deterministic mulberry32 sequence, so the same seed replays the same numbers. Call it with no argument to hand them back to Math.random().

ts
seed(value?: number): void

Parameters

  • valuenumber. Optional.

Returns

void

Example

js
import { Rand } from '@1pizzateam/spock';


const result = Rand.seed(1);

Rand.create()

Independent generator from seed.

Returns an independent generator carrying its own float, integer, distribution, and pick. Prefer it over seed() when you want reproducibility in one place without changing behaviour everywhere else.

ts
create(seed: number): { float(min, max): number; integer(min, max): number; distribution(min, max, iterations): number; pick(value1, value2): number }

Parameters

  • seednumber.

Returns

{ float(min, max): number; integer(min, max): number; distribution(min, max, iterations): number; pick(value1, value2): number }

Example

js
import { Rand } from '@1pizzateam/spock';


const result = Rand.create(42);

Released under the MIT License.