npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

toosoon-prng

v3.0.0

Published

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

Downloads

66

Readme

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.