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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@xtia/alea

v1.1.2

Published

A practical randomness toolkit. Sample/shuffle arrays, generate strings, create UUIDs, and more, with a unified API for Math.random(), crypto, or custom/transformed PRNGs.

Downloads

449

Readme

Alea

Tame the Chaos

Alea is a utility wrapper for turning random numbers into useful values. Give it any source of randomness, get a toolkit for dice, samples, strings, and more.

  • Expressive: code with intent
  • Crypto-safe and seeded algorithms out-of-the-box
  • Array shuffling, weighted sampling, recursive template phrase generation, UUID, bytes and more
  • Fully typed
  • No dependencies
  • ~2.4kb minified core

Basics

Install: npm i @xtia/alea

import { alea } from "@xtia/alea";

// default alea draws from Math.random()

// game development
const damage = alea.between(10, 20);
const crit = alea.chance(0.1);
const loot = alea.sample(['sword', 'potion', 'gold']);
const enemySpawner = alea.createWeightedSampler([
    ["goblin", 10],
    ["orc", 5],
    ["dragon", 0.1] // rare but possible!
]);
const enemy = enemySpawner.sample();

// data generation
const userId = alea.string(8, 'abcdef0123456789');
const fakeName = alea.phrase(nameTables, '{firstName} {lastName}');

// statistics
const [z1, z2] = alea.normal(0, 1);

For convenience, common character sets for string can be imported: import { charsets } from "@xtia/alea".

Crypto-secure

Use the same API layer, whatever the source of randomness:

import { cryptoAlea } from "@xtia/alea";

// cryptoAlea draws from runtime's crypto

const id = cryptoAlea.uuid();
const unpredictableDeck = cryptoAlea.shuffle(cards);

Custom sources

Use any randomness provider:

import {
    aleaFromFunc,
    aleaFromSeed,
    aleaFromByteSource,
    aleaFromSequence
} from "@xtia/alea";

// deterministic, with seed (uses Mulberry32 PRNG):
const seededRng = aleaFromSeed("abc123");

// custom source of randomness:
const xkcdRng = aleaFromFunc(() => 4/6); // https://xkcd.com/221/

// from third-party PRNGs
import { MersenneTwister } from "acme-math";
const mt = new MersenneTwister(mySeed);
const mtAlea = aleaFromFunc(() => mt.random());

// from random byte providers:
const secureRng = aleaFromByteSource(
    buf => hardwareRng.fillRandomBytes(buf)
);

// preset sequence for debugging/testing
const presetRng = aleaFromSequence([.1, .4, .8], "loop");

Or use a provided PRNG algorithm:

import {
    mulberry32,
    sfc32,
    xoshiro128pp,
} from "@xtia/alea/prng";

// each returns an Alea instance:
const fast = mulberry32("my-seed");
const varied = sfc32(1, 2, 3, 4);
const strong = xoshiro128pp(5, 6, 7, 8);

const reproducibleId = varied.string(12, hexadecimal);

Advanced: Probability Density Transforms

Transform randomness for different probability curves:

// exponential distribution
const expSampler = alea.transform(x => -Math.log(1 - x));
const waitTime = expSampler.between(0, 10);

// quadratic ease
const leftSkewed = alea.transform(x => x * x)
    .sample(items);

// square root ease
const rightSkewed = alea.transform(x => Math.sqrt(x))
    .sample(items);

Technicalities