Rand
Random numbers, either from Math.random() or from a seeded generator you can replay.
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.
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 middleRand.float()
Uniform float in [min, max).
Uniform in [min, max): min can come up, max cannot.
float(min: number, max: number): numberParameters
min—number.max—number.
Returns
number — the result
Example
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().
integer(min: number, max: number): numberParameters
min—number.max—number.
Returns
number — the result
Example
import { Rand } from '@1pizzateam/spock';
const result = Rand.integer(1, 1);2D particle distribution
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. 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.
distribution(min: number, max: number, iterations: number): numberParameters
min—number.max—number.iterations—number.
Returns
number — the result
Example
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.
pick(value1: number, value2: number): numberParameters
value1—number.value2—number.
Returns
number — the result
Example
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().
seed(value?: number): voidParameters
value—number. Optional.
Returns
void
Example
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.
create(seed: number): { float(min, max): number; integer(min, max): number; distribution(min, max, iterations): number; pick(value1, value2): number }Parameters
seed—number.
Returns
{ float(min, max): number; integer(min, max): number; distribution(min, max, iterations): number; pick(value1, value2): number }
Example
import { Rand } from '@1pizzateam/spock';
const result = Rand.create(42);