Skip to content

Choosing an API

There is one name per function. ransu.integer, the named export integer and Random#integer are the same function object, not three wrappers.

// 1. Namespace — the shortest thing to type.
import ransu from "ransu";
ransu.integer(1, 6);
// 2. Named exports — tree-shakeable.
import { integer, pick, shuffle } from "ransu";
integer(1, 6);
// 3. An instance — reproducible, and isolated from everyone else.
import { Random } from "ransu";
new Random(42).integer(1, 6);
Situation Use
A script, a prototype, a test fixture the namespace
Application code where bundle size matters named exports
A library you publish an instance, always
Anything that must replay identically an instance
Tokens, invite codes, prize draws ransu/secure

The namespace and the named exports both draw from one global stream, and any application can re-seed it:

import { seed } from "ransu";
seed(42);

That is a feature for applications and a hazard for libraries. If your package draws from the global, an application seeding it changes your behaviour. Own a new Random() instead and nobody can reach it.

Named exports use plain words — integer, sample, range, string. That keeps one name per function, at the cost of importing common identifiers into your scope. If that collides, import the namespace or alias at the import site:

import { integer as randomInteger } from "ransu";