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 🙏

© 2026 – Pkg Stats / Ryan Hefner

substrate-rng

v0.0.1

Published

Deterministic RNG from substrate state

Readme

substrate-rng

Seedable Random Number Generators. Deterministic, byte-exact across platforms.

Three RNGs:

import { Xoshiro256, PCG64, SplitMix64, rng, stringToSeed,
  UniformInt, Bernoulli, Exponential, Poisson, latinHypercube } from 'substrate-rng';

// Xoshiro256** — default. Fast, high-quality, 256-bit state. Period 2^256 - 1.
const r = new Xoshiro256(42n);
r.next();              // float in [0, 1)
r.nextInt(100);        // int in [0, 100)
r.nextRange(5, 15);    // int in [5, 15]
r.nextGaussian(0, 1);  // standard normal
r.shuffle(arr);        // Fisher-Yates in-place
r.serialize();         // save state for replay
Xoshiro256.deserialize(state);

// Seed from anything
new Xoshiro256('cell-witness');  // string → 64-bit seed via FNV-1a
new Xoshiro256(42n);             // raw integer seed
new Xoshiro256(timeSeed());      // from Date.now() + hrtime

// Distributions
new UniformInt(0, 100, r).sample();
new Bernoulli(0.3, r).sample();
new Exponential(2.0, r).sample();
new Poisson(5, r).sample();

// Quasi-random
const samples = latinHypercube(100, 4, r);

The math

Xoshiro256** (Blackman & Vigna 2018)

State: four 64-bit words (s0, s1, s2, s3). Period 2^256 - 1.

output = rotl((s1 * 5) rotl 7) * 9
t = s1 << 17
s2 ^= s0; s3 ^= s1; s1 ^= s2; s0 ^= s3; s2 ^= t; s3 = rotl(s3, 45)

Multiplier 5 = 0b101 comes from a 5-cycle LFSR. Rotation amounts (7, 45) are tuned for bit diffusion. The ** suffix means the output function is a "scrambler" — multiply by 9 to break linear structure.

Passes BigCrush (TestU01, the standard rigorous test suite). No known failures. Used in Julia, Nim, V language standard libraries.

PCG64 (O'Neill 2014)

State: 128-bit (state, inc). Period 2^128.

state = state * 6364136223846793005 + inc  (mod 2^64)
XSH-RR output: randomize then permute bits

XSH-RR = "XorShift High bits, Random Rotate" — outputs high bits of state, then rotates by a random amount derived from the stream parameter. Permutation makes the output pass strong statistical tests.

Period 2^128 means at 10^12 outputs/second it would take ~10^19 years to repeat.

SplitMix64 (Vigna 2014)

Minimal 64-bit RNG. Used as a building block to seed Xoshiro256 from a single u64:

state += 0x9e3779b97f4a7c15   // golden ratio constant
state = mix(state)             // bijective 64-bit transform

The golden ratio constant comes from a Knuth-style uniform sequence design.

Distributions

| Distribution | Algorithm | |--------------|-----------| | UniformInt | Inverse CDF: floor(rng() * (hi - lo + 1)) + lo | | Bernoulli | Uniform comparison: rng() < p | | Gaussian | Box-Muller: √(-2 ln u1) · cos(2π u2), then shift+scale | | Exponential | Inverse CDF: -ln(1 - rng()) / λ | | Poisson | Knuth for λ < 30, normal approximation otherwise | | Latin Hypercube | One permutation per dimension, jitter within cells |

Why seedable + deterministic?

Quilt cells need to be reproducible. Every cell's randomness is derived from its state hash. So we need an RNG that:

  • Same seed → same sequence (byte-exact)
  • Fast (called billions of times in a hash chain)
  • High quality (no statistical bias)
  • Small state (64-256 bits)

Xoshiro256** fits all four. PCG64 is the alternative when you want statistical excellence over speed.

Why FNV-1a for string seeds?

stringToSeed is a 64-bit hash function. FNV-1a is non-cryptographic, deterministic, and fast. The seed becomes a single u64 that drives Xoshiro256** via SplitMix64.

Don't use FNV-1a for cryptographic seeds. Use crypto.getRandomValues() (32 bytes) + import as BigInt.

License

MIT.