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

@trinkets/random

v0.0.9

Published

seeded psueudo random number generator

Downloads

4

Readme

@trinkets/random

Seeded, pseudorandom number generator. The main export is random which can be a drop in replacement for Math.random.

Good for simple video games, terrain generation, texture generation, or times when you need to make use of a random number generator that can produce the same results over different runs (via the initial seed).

Note from author: I do not know enough about cryptography to speak about the suitability of this code for that application. Let's say it's not suitable. Please don't use this for encryption. Please find another solution when you need strong, unpredictable hashing.

Installation

npm install @trinkets/random

Usage

import {choice, choices, create, random, randint, sample, seed, state} from '@trinkets/random'
// or import * as random from '@trinkets/random'

// Substitute for Math.random()
console.log(random())
// For when you want integers, here 1 <= result <= 10 (i.e. min <= x <= max)
console.log(randint(1, 2))

// Choose an element from a sequence.
console.log(choice(['cat', 42, 'cow']))
// Choose a random set of elements from a list (can have repeats).
console.log(choices(['cat', 42, 'cow'], 2))
// Choose a random set of weighted elements from a list (can have repeats).
console.log(choices(['cat', 42, 'cow'], 2, [10, 1, 2]))
// Choose a random set of elements from a list, no repeats.
console.log(sample(['cat', 42, 'cow'], 2))

// What is the initial seed used
console.log(seed.get())
// Reset the seed with a non-zero integer.
console.log(seed.set(132131232))

// Get the internal state as an object literal (`JSON.stringify`able).
const restorePoint = state.get()
// Make some random numbers or do other things.
// ...
// Restore the state back to whenever you took it. Any random numbers generated
// will be regenerated from the restorePoint.
state.set(restorePoint)

// When you need to manage the state of a separate random number generator.
const rng2 = create({
    // Optional: pass a function that will be called to generate the default
    // seed. The default uses Date.now().
    seedInitFn = () => Date.now()
})
// Same API.
console.log(rng2.random())
console.log(rng2.randint(1, 10))

Notes

This code originally started with an older version of the multiply-with-carry wikipedia article. The original code was:

m_w = /* choose some initializer, must not be zero */;
m_z = /* choose some initializer, must not be zero */;

uint get_random()
{
    m_z = 36969 * (m_z & 65535) + (m_z >> 16);
    m_w = 18000 * (m_w & 65535) + (m_w >> 16);
    return (m_z << 16) + m_w;  /* 32-bit result */
}

For the times I've needed to reproduce my number generation with a seed, this code has been good enough.

Some not-rigorous-tests on jsperf within my Chrome browser show this code to be about 20% faster than Math.random. Some additional not-rigorous-monte-carlo-tests (millions of runs) show the distribution of Math.random and this to have similar distributions of numbers.