# toosoon-prng

> This project provides PRNG functions for generating pseudo-random values using a seed-based approach

Latest version **5.0.0** (published 2026-02-09) · MIT license · 0 weekly downloads

## Install

```sh
npm install toosoon-prng
pnpm add toosoon-prng
yarn add toosoon-prng
bun add toosoon-prng
```

## Health

**Score 60/100 (C)** — status: stable.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 5.0.0 |
| Published | 2026-02-09 |
| First published | 2024-02-19 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=22 |
| Dependencies | 1 |
| Unpacked size | 35.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Arnaud Rocca |
| Maintainers | arnaudrocca |
| Keywords | toosoon, prng, pseudo-random, random, randomization, procedural, generative |

## Links

- npm: https://www.npmjs.com/package/toosoon-prng
- Repository: https://github.com/toosoon-dev/toosoon-prng
- Homepage: https://github.com/toosoon-dev/toosoon-prng#readme
- Issues: https://github.com/toosoon-dev/toosoon-prng/issues
- npm.io page: https://npm.io/package/toosoon-prng

## Dependencies (1)

- [toosoon-utils](https://npm.io/package/toosoon-utils.md) >=5.0.0

## Recent versions

- 5.0.0 (latest) — 2026-02-09
- 4.1.0 — 2026-01-31
- 4.0.4 — 2026-01-12
- 4.0.3 — 2025-03-27
- 4.0.2 — 2025-03-27
- 4.0.1 — 2025-03-24
- 4.0.0 — 2025-03-20
- 3.1.0 — 2025-03-19
- 3.0.2 — 2025-03-19
- 3.0.1 — 2025-02-02
- 3.0.0 — 2024-03-19
- 2.5.0 — 2024-03-12
- 2.4.0 — 2024-03-11
- 2.2.0 — 2024-03-08
- 2.1.0 — 2024-03-08
- … 15 more at https://npm.io/package/toosoon-prng/versions

## README

# TOOSOON Pseudo-Random Number Generator (PRNG)

This project provides PRNG functions for generating pseudo-random values using a seed-based approach. These are particularly useful for applications requiring deterministic randomization, such as procedural generation or simulations.

[![NPM](https://nodei.co/npm/toosoon-prng.png)](https://nodei.co/npm/toosoon-prng/)

## Installation

Yarn:

```properties
$ yarn add toosoon-prng
```

NPM:

```properties
$ npm install toosoon-prng
```

## Usage

Using default PRNG instance.

```ts
import prng from 'toosoon-prng';

prng.setSeed('seed');

prng.randomFloat('angle', 0, Math.PI); // Pseudo-random number in the interval [0, PI)
```

Using PRNG class constructor.

```ts
import { PRNG } from 'toosoon-prng';

const prng = new PRNG('seed');

prng.randomFloat('angle', 0, Math.PI); // Pseudo-random number in the interval [0, PI)
```

## API

### PRNG <a id="prng"></a>

Utility class for generating pseudo-random values.

- [new PRNG(seed?, generator?)](#prng-constructor)
	- [.seed](#prng-seed): `string`
	- [.generator](#prng-generator): `Function`
	- [.setSeed(seed?)](#prng-set-seed-method): `void`
	- [.random(subseed)](#prng-random-method): `number`
	- [.randomBoolean(subseed, probability?)](#prng-random-boolean-method): `boolean`
	- [.randomSign(subseed, probability?)](#prng-random-sign-method): `number`
	- [.randomFloat(subseed, min?, max?, precision?)](#prng-random-float-method): `number`
	- [.randomInt(subseed, min, max)](#prng-random-int-method): `number`
	- [.randomHexColor(subseed)](#prng-random-hex-color-method): `string`
	- [.randomItem(subseed, array)](#prng-random-item-method): `unknown|undefined`
	- [.randomObjectProperty(subseed, object)](#prng-random-object-property-method): `unknown|undefined`
	- [.randomIndex(subseed, weights)](#prng-random-index-method): `number`
	- [.randomGaussian(subseed, mean?, spread?)](#prng-random-gaussian-method): `number`

#### Types

```ts
type Seed = string | number;

type Generator = (...hashes: number[]) => number;
```

#### Constructor <a id="prng-constructor"></a>

| Parameter   | Type        | Default | Description                                            |
| ----------- | ----------- | ------- | ------------------------------------------------------ |
| [seed]      | `Seed`      |         | Seed string used for pseudo-random generations.        |
| [generator] | `Generator` | `sfc32` | Generator function used for pseudo-random generations. |

#### Properties

##### .`seed` <a id="prng-seed"></a>

Seed string used for pseudo-random generations.

```ts
PRNG.seed: string;
```

##### .`generator` <a id="prng-generator"></a>

Generator function used for pseudo-random generations.

```ts
PRNG.generator: Generator;
```

#### Methods

##### .`setSeed(seed)` <a id="prng-set-seed-method"></a>

Set the `PRNG` instance seed.

- `seed`

```ts
PRNG.setSeed(seed: Seed): void;
```

##### .`random(subseed)` <a id="prng-random-method"></a>

Generate a pseudo-random number between 0 and 1 (0 inclusive, 1 exclusive). 
Pseudo-random equivalent of `Math.random()`.

- `subseed`

```ts
PRNG.random(subseed: Seed): number;
```

##### .`randomBoolean(subseed, probability?)` <a id="prng-random-boolean-method"></a>

Generate a pseudo-random boolean (true or false).

- `subseed`
- `[probability=0.5]`: Probability to get true.

```ts
PRNG.randomBoolean(subseed: Seed, probability?: number): boolean;
```

##### .`randomSign(subseed, probability?)` <a id="prng-random-sign-method"></a>

Generate a pseudo-random sign (1 or -1).

- `subseed`
- `[probability=0.5]`: Probability to get 1.

```ts
PRNG.randomSign(subseed: Seed, probability?: number): number;
```

##### .`randomFloat(subseed, min?, max?, precision?)` <a id="prng-random-float-method"></a>

Generate a pseudo-random floating-point number within a specified range.

- `subseed`
- `[min=0]`: Minimum boundary.
- `[max=1]`: Maximum boundary.
- `[precison=2]`: Number of digits after the decimal point.

```ts
PRNG.randomFloat(subseed: Seed, min?: number, max?: number1, precision?: number): number;
```

##### .`randomInt(subseed, min, max)` <a id="prng-random-int-method"></a>

Generate a pseudo-random integer number within a specified range.

- `subseed`
- `min`: Minimum boundary.
- `max`: Maximum boundary.

```ts
PRNG.randomInt(subseed: Seed, min: number, max: number): void;
```

##### .`randomHexColor(subseed)` <a id="prng-random-hex-color-method"></a>

Generate a pseudo-random hexadecimal color.

- `subseed`

```ts
PRNG.randomHexColor(subseed: Seed): string;
```

##### .`randomItem(subseed, array)` <a id="prng-random-item-method"></a>

Pseudo-randomly pick an item from an array.

- `subseed`
- `array`: Array to pick the item from.

```ts
PRNG.randomItem<T>(subseed: Seed, array: T[]): T | undefined;
```

##### .`randomObjectProperty(subseed, object)` <a id="prng-random-object-property-method"></a>

Pseudo-randomly pick a property value from an object.

- `subseed`
- `object`: Object to pick the property from.

```ts
PRNG.randomObjectProperty<T>(subseed: Seed, object: Record<string, T>): T | undefined;
```

##### .`randomIndex(subseed, weights)` <a id="prng-random-index-method"></a>

Select a pseudo-random index from an array of weighted items.

- `subseed`
- `weights`: Array of weights.

```ts
PRNG.randomIndex(subseed: Seed, weights: number[]): number;
```

##### .`randomGaussian(subseed, mean?, spread?)` <a id="prng-random-gaussian-method"></a>

Generate a pseudo-random number fitting a Gaussian (normal) distribution.

- `subseed`
- `[mean=0]`: Mean (central) value of the distribution.
- `[spread=1]`: Spread (standard deviation) of the distribution.

```ts
PRNG.randomGaussian(subseed: Seed, mean?: number, spread?: number): number;
```

### Generators

By default, the library is using _SplitMix32_ generator for generating the pseudo-random values but it is possible to change the generator used by one of the following:

- `sfc32`: _Simple Fast Counter_, Generator with a 128-bit state.
- `jsf32`: _Jenkins' Small Fast_, Generator with a 128-bit state.
- `xoshiro128ss`: _xoshiro128\*\*_, Generator with a 128-bit state.
- `mulberry32`: _Mulberry32_, Generator with a 32-bit state.
- `splitmix32`: _SplitMix32_, Generator with a 32-bit state.

```ts
import { PRNG, sfc32 } from 'toosoon-prng';

const prng = new PRNG('seed', { generator: sfc32 });
```

### Utils

##### `randomSeed(length?)`

Generate a random string that can be used as a seed.

- `[length=64]`: Length of the generated seed string.

```ts
randomSeed(length?: number): string;
```

### Constants

`DEFAULT_SEED`

## License

MIT License, see [LICENSE](https://github.com/toosoon-dev/toosoon-prng/tree/master/LICENSE) for details.

---
_Source: https://npm.io/package/toosoon-prng · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
