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

@spissvinkel/alea

v1.2.1

Published

Seedable PRNG for JS/TS

Downloads

179

Readme

alea-ts

A seedable pseudo-random number generator based on Johannes Baagøes' Alea, written in Typescript.

API Documentation

TSDoc API reference

Installation

As an npm package:

$ npm install @spissvinkel/alea

Usage examples

Just generate some random numbers

The values returned are from 0 (inclusive) to 1 (exclusive), like Math.random()

import { mkAlea } from '@spissvinkel/alea';

const { random } = mkAlea();

// Probably different values every time you run the program
const myFirstRandomNumber = random();
const myNextRandomNumber = random();

Always generate the same sequence of random numbers by using a known seed value

import { mkAlea } from '@spissvinkel/alea';

const SEED_VALUE = '1234567890'; // Can be any string

const { random } = mkAlea(SEED_VALUE);

// Same values every time you run the program
const firstRandomNumberInTheSequence = random();
const secondRandomNumberInTheSequence = random();

Continue the sequence of random numbers at a later date

import { mkAlea, restoreAlea } from '@spissvinkel/alea';

const SEED_VALUE = '1234567890'; // Can be any string

const { random, getState } = mkAlea(SEED_VALUE);

// Same values every time you run the program
const firstRandomNumberInTheSequence = random();
const secondRandomNumberInTheSequence = random();

const state = getState(); // Retrieve the current state of the generator

// Maybe serialize and somehow save the state object for later
const serializedState = JSON.stringify(state);
// etc.

// Then later on, restore the generator and continue the sequence
const restoredState = JSON.parse(serializedState);
const { random: restoredRandom } = restoreAlea(restoredState);

// Same values every time you continue from the saved state
const thirdRandomNumberInTheSequence = restoredRandom();
const fourthRandomNumberInTheSequence = restoredRandom();

Generate other random values besides numbers between 0 and 1

You can create utility functions to generate random values of other types

import { mkAlea } from '@spissvinkel/alea';

const { nextT } = mkAlea();

// This creates a function that returns random booleans
const nextBool = nextT(n => n < 0.5);
const myFirstRandomBoolean = nextBool(); // True or false at random
const mySecondRandomBoolean = nextBool(); // True or false at random

// This creates a function that, given a `max` number, will create a second function which
// will generate random numbers from 0 up to, but not including, `max`
const mkNext0toMax = (max: number): () => number => nextT(n => Math.floor(n * max));

// This creates a function that returns random numbers from 0 up to, but not including, 100
const next0to100 = mkNext0toMax(100);
const x = next0to100(); // 0 <= x < 100 at random
const y = next0to100(); // 0 <= y < 100 at random

// This creates a function that, given a `values` array, will create a second function
// which will pick random elements from `values`
const mkNextElement = <T> (values: T[]): () => T => {
    const nextIndex = mkNext0toMax(values.length);
    return () => values[nextIndex()];
};

// This creates a function that returns an element of the colour array at random
const nextColour = mkNextElement([ 'red', 'green', 'blue' ]);
const firstColour = nextColour(); // Maybe 'green'
const secondColour = nextColour(); // Maybe 'red', or 'green' again

Advanced usage

If you need more fine grained control over your random sequences there are also some low-level functions available

import { AleaState, initState, mkState, nextT, random } from '@spissvinkel/alea';

const FOO_SEED = '12345';
const BAR_SEED = '67890';

const fooState = mkState(FOO_SEED);
const barState = mkState(BAR_SEED);

const myFirstFooValue = random(fooState);
const mySecondFooValue = random(fooState);
const myFirstBarValue = random(barState);
const myThirdFooValue = random(fooState);
const mySecondBarValue = random(barState);

const nextBool: (state: AleaState) => boolean = nextT(n => n < 0.5);

const myFourthFooValueIsABoolean = nextBool(fooState);
const myThirdBarValueIsAlsoABoolean = nextBool(barState);

// Reset state
initState(FOO_SEED, fooState);

const myFirstFooValueAgain = random(fooState); // Same as `myFirstFooValue`
const mySecondFooValueAgain = random(fooState); // Same as `mySecondFooValue`

Background

This project originally started as a fork of https://github.com/coverslide/node-alea.

See also https://github.com/nquinlan/better-random-numbers-for-javascript-mirror for more background information.