3.0.0 • Published 2 months ago

toosoon-prng v3.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
2 months ago

TOOSOON Pseudo-Random Number Generator (PRNG)

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

Installation

Yarn:

$ yarn add toosoon-prng

NPM:

$ npm install toosoon-prng

Usage

import prng from 'toosoon-prng';

prng.setSeed('seed');
console.log(prng.randomFloat('angle', 0, Math.PI)); // Pseudo-random number in the interval [0, PI]

PRNG Functions

All PRNG functions have a seed parameter wich allows you to add a sub-seed string to the PRNG instance global seed in order to get different pseudo-random values while using a PRNG function multiple times.

prng.setSeed('global-seed');
console.log(prng.randomBoolean('one')); // Could be true or false
console.log(prng.randomBoolean('two')); // Could be different from the first pseudo-random value
setSeed(seed)

Set the PRNG instance seed.

  • seed
prng.setSeed(seed: string): void;
setAlgorithm(algorithmName)

Set the PRNG algorithm for generating pseudo-random values.

  • algorithmName: Algorithm name.
prng.setAlgorithm(algorithmName: AlgorithmName): void;
random(seed)

Generate a pseudo-random number in the interval 0, 1. PRNG equivalent of Math.random().

  • seed
prng.random(seed: string): number;
randomBoolean(seed)

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

  • seed
  • [probability=0.5]: Probability to get true.
prng.randomBoolean(seed: string, probability?: number): boolean;
randomSign(seed)

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

  • seed
  • [probability=0.5]: Probability to get 1.
prng.randomSign(seed: string, probability?: number): number;
randomFloat(seed, min, max)

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

  • seed
  • [min=0]: Minimum boundary.
  • [max=1]: Maximum boundary.
  • [precison=2]: Number of digits after the decimal point.
prng.randomFloat(seed: string, min?: number, max?: number1, precision?: number): number;
randomInt(seed, min, max)

Generate pseudo-random integer number within a specified range.

  • seed
  • min: Minimum boundary.
  • max: Maximum boundary.
prng.randomInt(seed: string, min: number, max: number): void;
randomHexColor(seed)

Generate a pseudo-random hexadecimal color.

  • seed
prng.randomHexColor(seed: string): string;
randomItem(seed0 array)

Pick a pseudo-random item from a given array.

  • seed
  • array: Array to pick the item from.
prng.randomItem<T>(seed: string, array: T[]): T | undefined;
randomObjectProperty(seed, object)

Pick a pseudo-random property value from a given object.

  • seed
  • object: Object to pick the property from.
prng.randomObjectProperty<T>(seed: string, object: Record<string, T>): T | undefined;
randomIndex(seed, weights)

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

  • seed
  • weights: Array of weights
prng.randomIndex(seed: string, weights: number[]): number;

Algorithms

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

  • jsf32: Jenkins' Small Fast, Generator with a 32-bit state.
  • mulberry32: Mulberry32, Generator with a 32-bit state.
  • sfc32: Simple Fast Counter, Generator with a 128-bit state.
  • splitmix32: SplitMix32, Generator with a 32-bit state.
  • xoshiro128ss: xoshiro128**, Generator with a 128-bit state.
import prng, { Algorithm } from 'toosoon-prng';

// Set 'Simple Fast Counter' as the PRNG instance algorithm
prng.setAlgorithm(Algorithm.sfc32);

License

MIT License, see LICENSE for details.

3.0.0

2 months ago

2.5.0

2 months ago

2.4.0

2 months ago

2.2.0

2 months ago

2.1.0

2 months ago

2.0.0

2 months ago

1.5.0

3 months ago

1.2.3

3 months ago

1.4.0

3 months ago

1.2.2

3 months ago

1.3.0

3 months ago

1.2.1

3 months ago

1.2.0

3 months ago

1.1.0

3 months ago

1.0.5

3 months ago

1.0.4

3 months ago

1.0.3

3 months ago

1.0.2

3 months ago

1.0.1

3 months ago

1.0.0

3 months ago