# base/seededrandom (vanilla.js 0.4.0)

> https://vanillapunch.com/docs/vanilla.js/0.4.0/api/base/seededrandom/

Seeded random number generator. (mulberry32)

## SeededRandom extends Object

Seeded random number generator. (mulberry32)
- Deterministic random numbers that always produce the same sequence from the same seed.
- Used where reproducibility is needed, such as stage generation / replays / match synchronization.
- Standardizes the xorshift32 / mulberry32 that several games each implemented separately.

Example: Reproduce the same results with the same seed

```js
import { SeededRandom } from "./build/vanilla.js";

const seededRandom = new SeededRandom(12345);
const dice = seededRandom.nextInt(1, 6);
const angle = seededRandom.nextRange(0, 360);
const item = seededRandom.pick(["sword", "shield", "potion"]);
console.log(dice, angle, item);

const replay = new SeededRandom(12345);
const replayDice = replay.nextInt(1, 6);
console.log(replayDice === dice);
```

- `constructor(seed = 1)` (constructor, since 0.3.0): Constructor.
  - `seed` number: Seed. (converted to a 32-bit unsigned integer, 1 is used if 0)
- `setSeed(seed)` (method, since 0.3.0): Resets the seed.
  - `seed` number: Seed. (converted to a 32-bit unsigned integer, 1 is used if 0)
- `getState()` (method, since 0.3.0): Returns the current internal state. (for save / restore)
  - returns number: Internal state. (32-bit unsigned integer)
- `setState(state)` (method, since 0.3.0): Restores the internal state.
  - `state` number: State saved with getState().

  Example: Save the state and restore it

  ```js
  import { SeededRandom } from "./build/vanilla.js";
  
  const seededRandom = new SeededRandom(99);
  const savedState = seededRandom.getState();
  const firstValue = seededRandom.nextValue();
  seededRandom.setState(savedState);
  const againValue = seededRandom.nextValue();
  console.log(firstValue === againValue);
  ```

- `nextValue()` (method, since 0.3.0): Returns a random number from 0 inclusive to 1 exclusive.
  - returns number: A random number from 0 inclusive to 1 exclusive.
- `nextRange(minValue, maxValue)` (method, since 0.3.0): Returns a random real number from min (inclusive) to max (exclusive).
  - `minValue` number: Lower bound. (inclusive)
  - `maxValue` number: Upper bound. (exclusive)
  - returns number: A real number from minValue inclusive to maxValue exclusive.
- `nextInt(minValue, maxValue)` (method, since 0.3.0): Returns a random integer from min to max (both inclusive).
  - `minValue` number: Lower bound. (integer, inclusive)
  - `maxValue` number: Upper bound. (integer, inclusive)
  - returns number: An integer from minValue to maxValue inclusive.
- `pick(array)` (method, since 0.3.0): Picks one from an array.
  - `array` T[]: Array to pick from.
  - returns T | undefined: The picked item. (undefined if the array is empty or missing)
- `shuffle(array)` (method, since 0.3.0): Shuffles an array in place. (Fisher-Yates)
  - `array` T[]: Array to shuffle. (changed in place)
  - returns T[]: The same array, shuffled.
- `static createDailySeed(dateNumber, baseSeed = 0x9E3779B1)` (static, since 0.3.0): Creates a date-based seed. (static)
  - Mixes a YYYYMMDD integer like 20260831, so the same date gives the same seed everywhere.
    Used for daily challenge board generation, etc.
  - `dateNumber` number: Date integer shaped YYYYMMDD. (like 20260831)
  - `baseSeed` number: Base seed to mix in. (default 0x9E3779B1, giving each game a different one yields different seeds on the same day)
  - returns number: Seed. (32-bit unsigned integer, 1 if 0)
- `static fromDate(date = new System.Date(), baseSeed = 0x9E3779B1)` (static, since 0.3.0): Creates a seeded random generator for today's date. (static)
  - `date` Date: Date. (default now, the year, month and day are read in the device's local time)
  - `baseSeed` number: Base seed to mix in. (default 0x9E3779B1, giving each game a different one yields different seeds on the same day)
  - returns SeededRandom: A seeded random generator made from that date's seed.

  Example: Make a daily challenge board

  ```js
  import { SeededRandom } from "./build/vanilla.js";
  
  const dailyRandom = SeededRandom.fromDate(new Date(2026, 7, 31));
  const dailySeed = SeededRandom.createDailySeed(20260831);
  const sameDayRandom = new SeededRandom(dailySeed);
  const boardValue = dailyRandom.nextInt(1, 100);
  const sameDayValue = sameDayRandom.nextInt(1, 100);
  console.log(boardValue === sameDayValue);
  ```

