RansuApi
Defined in: src/namespace/index.ts:86
The shape of the default export, derived from the object itself.
Extends
Section titled “Extends”Omit<typeofapi,"default">
RansuApi():
number
Defined in: src/namespace/index.ts:88
Shorthand for RansuApi.random.
Returns
Section titled “Returns”number
Properties
Section titled “Properties”AliasTable
Section titled “AliasTable”AliasTable: typeof
AliasTable
Defined in: src/namespace/index.ts:57
Inherited from
Section titled “Inherited from”Omit.AliasTable
alphabets
Section titled “alphabets”alphabets:
object
Defined in: src/namespace/index.ts:45
The named alphabets string() accepts, for building your own.
alphanumeric
Section titled “alphanumeric”
readonlyalphanumeric:string
readonlyascii: “!"#$%&‘()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}” = “!"#$%&’()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}”
base32
Section titled “base32”
readonlybase32:"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"="ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"
base32hex
Section titled “base32hex”
readonlybase32hex:"0123456789ABCDEFGHIJKLMNOPQRSTUV"="0123456789ABCDEFGHIJKLMNOPQRSTUV"
base58
Section titled “base58”
readonlybase58:"123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"="123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
base62
Section titled “base62”
readonlybase62:string
base64url
Section titled “base64url”
readonlybase64url:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"
binary
Section titled “binary”
readonlybinary:"01"="01"
digits
Section titled “digits”
readonlydigits:"0123456789"=DIGITS
readonlyhex:"0123456789abcdef"="0123456789abcdef"
hexUpper
Section titled “hexUpper”
readonlyhexUpper:"0123456789ABCDEF"="0123456789ABCDEF"
letters
Section titled “letters”
readonlyletters:string
readonlylower:"abcdefghijklmnopqrstuvwxyz"=LOWER
readonlyoctal:"01234567"="01234567"
unambiguous
Section titled “unambiguous”
readonlyunambiguous:"23456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"="23456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
readonlyupper:"ABCDEFGHIJKLMNOPQRSTUVWXYZ"=UPPER
Example
Section titled “Example”import { alphabets, string } from "ransu";
string(8, alphabets.lower); // "qmzdfhbx"string(8, alphabets.alphanumeric); // the defaultstring(8, alphabets.hex + alphabets.upper); // combine them as stringsInherited from
Section titled “Inherited from”Omit.alphabets
angle: () =>
number
Defined in: src/geometry.ts:142
A uniform angle in radians, in [0, 2pi).
Returns
Section titled “Returns”number
Example
Section titled “Example”angle(); // 2.4913...Inherited from
Section titled “Inherited from”Omit.angle
angleDegrees
Section titled “angleDegrees”angleDegrees: () =>
number
Defined in: src/geometry.ts:154
A uniform angle in degrees, in [0, 360).
Returns
Section titled “Returns”number
Example
Section titled “Example”angleDegrees(); // 142.74...Inherited from
Section titled “Inherited from”Omit.angleDegrees
backoff
Section titled “backoff”backoff: (
attempt,options) =>number
Defined in: src/time.ts:128
How long to wait before retry number attempt, counting from 0.
Retrying on a fixed schedule makes every client in a fleet retry at the same instant; the jitter strategies exist to break that up.
Parameters
Section titled “Parameters”attempt
Section titled “attempt”number
options?
Section titled “options?”BackoffOptions = {}
Returns
Section titled “Returns”number
Example
Section titled “Example”backoff(0); // 87.3 around 100msbackoff(1); // 213.9 around 200msbackoff(4); // 1483.2 around 1.6s, capped by maxDelay
backoff(3, { base: 250, factor: 3, jitter: "equal" });Inherited from
Section titled “Inherited from”Omit.backoff
bates: (
n) =>number
Defined in: src/global/distributions.ts:235
One draw: the mean of n uniforms.
Parameters
Section titled “Parameters”number
Returns
Section titled “Returns”number
Example
Section titled “Example”bates(10); // 0.5127...Inherited from
Section titled “Inherited from”Omit.bates
below: (
n) =>number
Defined in: src/global/numbers.ts:52
An integer in [0, n). The form array indices want.
Parameters
Section titled “Parameters”number
Returns
Section titled “Returns”number
Example
Section titled “Example”const items = ["a", "b", "c"];items[below(items.length)]; // "c"Inherited from
Section titled “Inherited from”Omit.below
bernoulli
Section titled “bernoulli”bernoulli: (
p) =>number
Defined in: src/global/distributions.ts:276
1 with probability p, otherwise 0.
Parameters
Section titled “Parameters”number = 0.5
Returns
Section titled “Returns”number
Example
Section titled “Example”bernoulli(); // 1bernoulli(0.25); // 0Inherited from
Section titled “Inherited from”Omit.bernoulli
beta: (
alpha,betaShape) =>number
Defined in: src/global/distributions.ts:77
One draw in (0, 1), shaped by two positive parameters.
Parameters
Section titled “Parameters”number
betaShape
Section titled “betaShape”number
Returns
Section titled “Returns”number
Example
Section titled “Example”beta(2, 5); // 0.2734... skewed toward 0Inherited from
Section titled “Inherited from”Omit.beta
bigBits
Section titled “bigBits”bigBits: (
n) =>bigint
Defined in: src/global/numbers.ts:151
An integer built from n random bits, with no width limit.
Parameters
Section titled “Parameters”number
Returns
Section titled “Returns”bigint
Example
Section titled “Example”bigBits(256); // 8873184...nInherited from
Section titled “Inherited from”Omit.bigBits
bigint
Section titled “bigint”bigint: (
min,max) =>bigint
Defined in: src/global/numbers.ts:78
A bigint in [min, max], both ends included.
Parameters
Section titled “Parameters”bigint
bigint
Returns
Section titled “Returns”bigint
Example
Section titled “Example”bigint(0n, 2n ** 128n); // 214703556478...nInherited from
Section titled “Inherited from”Omit.bigint
binomial
Section titled “binomial”binomial: (
n,p) =>number
Defined in: src/global/distributions.ts:301
The number of successes in n independent trials.
Parameters
Section titled “Parameters”number
number
Returns
Section titled “Returns”number
Example
Section titled “Example”binomial(10, 0.5); // 6Inherited from
Section titled “Inherited from”Omit.binomial
bits: (
n) =>number
Defined in: src/global/numbers.ts:139
An integer built from n random bits, up to 53.
Parameters
Section titled “Parameters”number
Returns
Section titled “Returns”number
Example
Section titled “Example”bits(8); // 173 an integer in [0, 256)bits(53); // 6519085048561357Inherited from
Section titled “Inherited from”Omit.bits
bool: () =>
boolean
Defined in: src/global/numbers.ts:90
true or false, evenly.
Returns
Section titled “Returns”boolean
Example
Section titled “Example”bool(); // trueInherited from
Section titled “Inherited from”Omit.bool
bucket
Section titled “bucket”bucket: (
key,buckets,salt?) =>number
Defined in: src/hash.ts:80
A stable bucket in [0, buckets) for key. Evenly spread across keys.
Parameters
Section titled “Parameters”buckets
Section titled “buckets”number
Returns
Section titled “Returns”number
Example
Section titled “Example”bucket("user-42", 16); // 11, the shard this user always lands inInherited from
Section titled “Inherited from”Omit.bucket
bytes: (
n) =>Uint8Array
Defined in: src/global/numbers.ts:163
n random bytes.
Parameters
Section titled “Parameters”number
Returns
Section titled “Returns”Uint8Array
Example
Section titled “Example”bytes(16); // Uint8Array(16) [ 57, 129, 123, ... ]Inherited from
Section titled “Inherited from”Omit.bytes
categorical
Section titled “categorical”categorical: (
weights) =>number
Defined in: src/global/distributions.ts:289
An index in [0, weights.length), proportional to the weights.
Parameters
Section titled “Parameters”weights
Section titled “weights”ArrayLike<number>
Returns
Section titled “Returns”number
Example
Section titled “Example”categorical([1, 3, 6]); // 2, most of the timeInherited from
Section titled “Inherited from”Omit.categorical
cauchy
Section titled “cauchy”cauchy: (
location,scale) =>number
Defined in: src/global/distributions.ts:125
One Cauchy draw. It has no mean, so expect extreme outliers.
Parameters
Section titled “Parameters”location?
Section titled “location?”number = 0
scale?
Section titled “scale?”number = 1
Returns
Section titled “Returns”number
Example
Section titled “Example”cauchy(); // -0.4142... and occasionally something enormousInherited from
Section titled “Inherited from”Omit.cauchy
chance
Section titled “chance”chance: (
p) =>boolean
Defined in: src/global/numbers.ts:102
true with probability p.
Parameters
Section titled “Parameters”number
Returns
Section titled “Returns”boolean
Example
Section titled “Example”chance(0.25); // true a quarter of the timeInherited from
Section titled “Inherited from”Omit.chance
char: (
options?) =>string
Defined in: src/global/strings.ts:58
A uniformly chosen character, as a string of one code point.
Parameters
Section titled “Parameters”options?
Section titled “options?”Returns
Section titled “Returns”string
Example
Section titled “Example”char(); // "符"char({ blocks: "emoji" }); // an emojichar({ blocks: ["hiragana", "katakana"] }); // "ネ"Inherited from
Section titled “Inherited from”Omit.char
chars: (
length,options?) =>string
Defined in: src/global/strings.ts:74
A random string of length code points, not UTF-16 units.
An astral character costs two UTF-16 units, so chars(10) can return a
string whose .length is more than ten. The count is of characters.
Parameters
Section titled “Parameters”length
Section titled “length”number
options?
Section titled “options?”Returns
Section titled “Returns”string
Example
Section titled “Example”chars(8, { blocks: "kana" }); // "へソむリあヴんケ"chars(5, { blocks: "emoji" }).length; // 10, five astral charactersInherited from
Section titled “Inherited from”Omit.chars
chiSquared
Section titled “chiSquared”chiSquared: (
df) =>number
Defined in: src/global/distributions.ts:89
One chi-squared draw with df degrees of freedom.
Parameters
Section titled “Parameters”number
Returns
Section titled “Returns”number
Example
Section titled “Example”chiSquared(3); // 2.3661...Inherited from
Section titled “Inherited from”Omit.chiSquared
choices
Section titled “choices”choices: <
T>(items,k) =>T[]
Defined in: src/global/collections.ts:80
k elements with replacement, so k may exceed the collection size.
Type Parameters
Section titled “Type Parameters”T
Parameters
Section titled “Parameters”Collection<T>
number
Returns
Section titled “Returns”T[]
Example
Section titled “Example”choices(["a", "b"], 5); // [ "b", "b", "a", "b", "a" ]Inherited from
Section titled “Inherited from”Omit.choices
codePoint
Section titled “codePoint”codePoint: (
options?) =>number
Defined in: src/global/strings.ts:44
A uniformly chosen Unicode code point. Defaults to the printable blocks.
Parameters
Section titled “Parameters”options?
Section titled “options?”Returns
Section titled “Returns”number
Example
Section titled “Example”codePoint(); // 12441codePoint({ blocks: "hiragana" }); // 12395Inherited from
Section titled “Inherited from”Omit.codePoint
CodePointSet
Section titled “CodePointSet”CodePointSet: typeof
CodePointSet
Defined in: src/namespace/index.ts:58
Inherited from
Section titled “Inherited from”Omit.CodePointSet
coin: () =>
"heads"|"tails"
Defined in: src/dice.ts:206
Heads or tails, for when a boolean would read worse at the call site.
Returns
Section titled “Returns”"heads" | "tails"
Example
Section titled “Example”coin(); // "heads"Inherited from
Section titled “Inherited from”Omit.coin
color: (
options) =>string
Defined in: src/color.ts:147
A CSS colour string.
Hex by default. Opacity only appears when alpha was asked for, so the
common case stays #rrggbb.
Parameters
Section titled “Parameters”options?
Section titled “options?”ColorOptions = {}
Returns
Section titled “Returns”string
Example
Section titled “Example”color(); // "#3e81d6"color({ format: "rgb" }); // "rgb(62 129 214)"color({ format: "hsl" }); // "hsl(214 62% 55%)"
color({ alpha: true }); // "#3e81d64f" a random alphacolor({ alpha: 0.5 }); // "#3e81d680" a fixed onecolor({ format: "rgb", alpha: 0.4 }); // "rgb(62 129 214 / 0.4)"
// Constrain any channel to a value or an inclusive range.color({ hue: [200, 240], saturation: 0.7, lightness: [0.3, 0.5] });Inherited from
Section titled “Inherited from”Omit.color
combination
Section titled “combination”combination: <
T>(items,k) =>T[]
Defined in: src/global/collections.ts:104
k distinct elements, kept in their original order.
Type Parameters
Section titled “Type Parameters”T
Parameters
Section titled “Parameters”Collection<T>
number
Returns
Section titled “Returns”T[]
Example
Section titled “Example”combination([1, 2, 3, 4, 5], 3); // [ 1, 4, 5 ]Inherited from
Section titled “Inherited from”Omit.combination
d10: (
count?) =>number
Defined in: src/dice.ts:165
A 10-sided die, or count of them summed.
Parameters
Section titled “Parameters”count?
Section titled “count?”number
Returns
Section titled “Returns”number
Example
Section titled “Example”d10(); // 7d10(2); // the sum of twoInherited from
Section titled “Inherited from”Omit.d10
d100: (
count?) =>number
Defined in: src/dice.ts:196
A percentile die: 1 to 100.
Parameters
Section titled “Parameters”count?
Section titled “count?”number
Returns
Section titled “Returns”number
Example
Section titled “Example”d100(); // 73Inherited from
Section titled “Inherited from”Omit.d100
d12: (
count?) =>number
Defined in: src/dice.ts:175
A 12-sided die, or count of them summed.
Parameters
Section titled “Parameters”count?
Section titled “count?”number
Returns
Section titled “Returns”number
Example
Section titled “Example”d12(); // 9d12(2); // the sum of twoInherited from
Section titled “Inherited from”Omit.d12
d20: (
count?) =>number
Defined in: src/dice.ts:187
A 20-sided die, or count of them summed.
The one a critical hit is measured against.
Parameters
Section titled “Parameters”count?
Section titled “count?”number
Returns
Section titled “Returns”number
Example
Section titled “Example”d20(); // 17d20(2); // the sum of twoInherited from
Section titled “Inherited from”Omit.d20
d4: (
count?) =>number
Defined in: src/dice.ts:132
A four-sided die, or count of them summed.
Parameters
Section titled “Parameters”count?
Section titled “count?”number
Returns
Section titled “Returns”number
Example
Section titled “Example”d4(); // 3d6(3); // 11, the sum of three d6d20(); // 17Inherited from
Section titled “Inherited from”Omit.d4
d6: (
count?) =>number
Defined in: src/dice.ts:145
A six-sided die, or count of them summed.
d4 through d100 are the same shape; reach for dice when the
notation comes from data rather than from your own code.
Parameters
Section titled “Parameters”count?
Section titled “count?”number
Returns
Section titled “Returns”number
Example
Section titled “Example”d6(); // 4d6(3); // 11, the sum of threeInherited from
Section titled “Inherited from”Omit.d6
d8: (
count?) =>number
Defined in: src/dice.ts:155
A 8-sided die, or count of them summed.
Parameters
Section titled “Parameters”count?
Section titled “count?”number
Returns
Section titled “Returns”number
Example
Section titled “Example”d8(); // 5d8(2); // the sum of twoInherited from
Section titled “Inherited from”Omit.d8
date: (
from,to) =>Date
Defined in: src/time.ts:24
A Date in [from, to).
Parameters
Section titled “Parameters”Returns
Section titled “Returns”Date
Example
Section titled “Example”date(new Date("2020-01-01"), new Date("2030-01-01")); // 2026-08-23T09:12:44.019Zdate(0, Date.now()); // any moment since the epoch; numbers are epoch millisInherited from
Section titled “Inherited from”Omit.date
default
Section titled “default”default:
RansuApi
Defined in: src/namespace/index.ts:90
Present so require('ransu').default also works.
dice:
DiceApi
Defined in: src/dice.ts:98
Roll dice written the way people already write them.
dice() gives the total; dice.detail() also reports what each die
showed, for a log or an on-screen roll.
Example
Section titled “Example”dice("3d6"); // 11dice("d20"); // 17dice("2d10+3"); // 14dice("1d4-1"); // 2
dice.detail("4d6+2"); // { total: 17, dice: [ 3, 6, 2, 4 ], modifier: 2 }Inherited from
Section titled “Inherited from”Omit.dice
dirichlet
Section titled “dirichlet”dirichlet: (
alpha) =>number[]
Defined in: src/global/distributions.ts:262
One point on the simplex: values in (0, 1) that sum to 1.
Parameters
Section titled “Parameters”readonly number[]
Returns
Section titled “Returns”number[]
Example
Section titled “Example”dirichlet([1, 1, 1]); // [ 0.24, 0.51, 0.25 ]Inherited from
Section titled “Inherited from”Omit.dirichlet
distributions
Section titled “distributions”distributions:
distribution
Defined in: src/namespace/index.ts:50
Inherited from
Section titled “Inherited from”Omit.distributions
duration
Section titled “duration”duration: (
min,max) =>number
Defined in: src/time.ts:66
A duration in milliseconds, in [min, max).
Parameters
Section titled “Parameters”number
number
Returns
Section titled “Returns”number
Example
Section titled “Example”duration(500, 2_000); // 1342Inherited from
Section titled “Inherited from”Omit.duration
engine
Section titled “engine”engine: () =>
Engine
Defined in: src/global/index.ts:5
The engine every top-level function currently draws from.
Returns
Section titled “Returns”Example
Section titled “Example”engine().algorithm; // "Math.random" until you call seed()Inherited from
Section titled “Inherited from”Omit.engine
engines
Section titled “engines”engines:
object
Defined in: src/namespace/index.ts:51
Every engine ransu ships, as seedable factories.
chacha20
Section titled “chacha20”chacha20: (
seed?) =>ChaCha20
ChaCha20 — a cryptographic stream cipher used as a generator.
The only engine that is both seedable and unpredictable: knowing any
number of outputs does not reveal the rest. Use it when a stream must be
reproducible and resistant to prediction; for secrets with no
reproducibility requirement, ransu/secure is simpler.
Parameters
Section titled “Parameters”Returns
Section titled “Returns”Example
Section titled “Example”const source = chacha20(12345);source.nextUint32(); // 1274611588
// Reproducible, unlike the platform CSPRNG.chacha20(1).nextUint32() === chacha20(1).nextUint32(); // truecryptoRandom
Section titled “cryptoRandom”cryptoRandom:
CryptoEngine
The platform CSPRNG, wrapped as an engine.
crypto.getRandomValues in browsers, workers and edge runtimes;
node:crypto on Node. Unpredictable, and for that reason not seedable —
which is what backs ransu/secure and the identifier functions.
Example
Section titled “Example”cryptoRandom.nextUint32(); // unpredictablecryptoRandom.seedable; // false
new Random(cryptoRandom).pick(["a", "b", "c"]);mt19937
Section titled “mt19937”mt19937: typeof
mt19937
mulberry32
Section titled “mulberry32”mulberry32: (
seed?) =>Mulberry32
mulberry32 — 32 bits of state, a dozen lines of arithmetic.
The smallest engine here. Its 2^32 period is short enough to matter in a long run, so prefer it only where the state size is the constraint.
Parameters
Section titled “Parameters”Returns
Section titled “Returns”Example
Section titled “Example”const source = mulberry32(42);source.nextUint32(); // 2744357186nativeMath
Section titled “nativeMath”nativeMath:
NativeMathEngine
Math.random, wrapped as an engine.
Not seedable and not reproducible: the host owns the state. This is what
the global functions draw from until you call seed().
Example
Section titled “Example”nativeMath.nextUint32(); // 2166136261nativeMath.seedable; // falsepcg32: (
seed?) =>Pcg32
PCG32 — a 64-bit LCG whose output is permuted before it is returned.
Statistically excellent for its size, and it supports multiple independent streams from one seed.
Parameters
Section titled “Parameters”Returns
Section titled “Returns”Example
Section titled “Example”const source = pcg32(42);source.nextUint32(); // 2722782280sfc32: (
seed?) =>Sfc32
sfc32 — Small Fast Counting, 128 bits of state.
About as fast as anything here and very small. Chosen when speed matters more than a proven period.
Parameters
Section titled “Parameters”Returns
Section titled “Returns”Example
Section titled “Example”const source = sfc32(42);source.nextUint32(); // 261194151xoshiro128pp
Section titled “xoshiro128pp”xoshiro128pp: (
seed?) =>Xoshiro128pp
xoshiro128++ — 128 bits of state, the default engine.
Fast, small, and passes BigCrush. Its period of 2^128-1 is far more than any single program will draw, but it is not cryptographic: a few outputs are enough to recover the state and predict the rest.
Parameters
Section titled “Parameters”Returns
Section titled “Returns”Example
Section titled “Example”const source = xoshiro128pp(42);source.nextUint32(); // 167929222
new Random(source).integer(1, 6); // 3xoshiro256pp
Section titled “xoshiro256pp”xoshiro256pp: (
seed?) =>Xoshiro256pp
xoshiro256++ — 256 bits of state.
The same family as xoshiro128pp with a longer period, and the one
to pick when you need jump() to hand disjoint streams to parallel
workers.
Parameters
Section titled “Parameters”Returns
Section titled “Returns”Example
Section titled “Example”const source = xoshiro256pp(42);source.nextUint32(); // 1573169414
// Non-overlapping streams for parallel work.const [a, b, c] = source.split(3);Example
Section titled “Example”// Pick one for a Random, or for the global stream.new Random(42, { engine: engines.pcg32 });
// Or build one directly and hand it around.const source = engines.xoshiro256pp(42);source.nextUint32(); // 1573169414
engines.mt19937(42); // for reproducing another language's streamengines.chacha20(42); // cryptographic, and still reproducibleengines.cryptoRandom; // the platform CSPRNG, not seedableengines.nativeMath; // Math.random, not seedableInherited from
Section titled “Inherited from”Omit.engines
exponential
Section titled “exponential”exponential: (
rate) =>number
Defined in: src/global/distributions.ts:38
One exponential draw: the wait until the next event.
Parameters
Section titled “Parameters”number = 1
Returns
Section titled “Returns”number
Example
Section titled “Example”exponential(); // 0.6931... rate 1exponential(0.5); // 3.2188... the mean is 1 / rateInherited from
Section titled “Inherited from”Omit.exponential
fillBytes
Section titled “fillBytes”fillBytes: (
out) =>void
Defined in: src/global/numbers.ts:176
Fill an existing buffer, with no allocation.
Parameters
Section titled “Parameters”Uint8Array
Returns
Section titled “Returns”void
Example
Section titled “Example”const buffer = new Uint8Array(32);fillBytes(buffer);Inherited from
Section titled “Inherited from”Omit.fillBytes
fisherF
Section titled “fisherF”fisherF: (
d1,d2) =>number
Defined in: src/global/distributions.ts:113
One F draw, the ratio of two chi-squared variables.
Parameters
Section titled “Parameters”number
number
Returns
Section titled “Returns”number
Example
Section titled “Example”fisherF(3, 10); // 1.1274...Inherited from
Section titled “Inherited from”Omit.fisherF
float: (
min?,max?) =>number
Defined in: src/global/numbers.ts:26
A double in [min, max), or [0, 1) with no arguments.
Parameters
Section titled “Parameters”number
number
Returns
Section titled “Returns”number
Example
Section titled “Example”float(); // 0.3964157...float(10); // 6.2831853... one argument is the upper boundfloat(-1.5, 1.5); // 0.4142135...Inherited from
Section titled “Inherited from”Omit.float
floats
Section titled “floats”floats: (
n) =>Float64Array
Defined in: src/global/numbers.ts:188
n doubles in [0, 1).
Parameters
Section titled “Parameters”number
Returns
Section titled “Returns”Float64Array
Example
Section titled “Example”floats(1_000); // Float64Array(1000) [ 0.396..., 0.364..., ... ]Inherited from
Section titled “Inherited from”Omit.floats
futureDate
Section titled “futureDate”futureDate: (
days) =>Date
Defined in: src/time.ts:53
A Date within the next days (default 7).
Parameters
Section titled “Parameters”number = 7
Returns
Section titled “Returns”Date
Example
Section titled “Example”futureDate(30); // some time in the next monthInherited from
Section titled “Inherited from”Omit.futureDate
gamma: (
shape,scale) =>number
Defined in: src/global/distributions.ts:52
One gamma draw.
Parameters
Section titled “Parameters”number
scale?
Section titled “scale?”number = 1
Returns
Section titled “Returns”number
Example
Section titled “Example”gamma(2); // 1.4726...gamma(2, 10); // 14.726... shape 2, scale 10Inherited from
Section titled “Inherited from”Omit.gamma
geometric
Section titled “geometric”geometric: (
p) =>number
Defined in: src/global/distributions.ts:326
The trial on which the first success lands, counting from 1.
Parameters
Section titled “Parameters”number
Returns
Section titled “Returns”number
Example
Section titled “Example”geometric(0.1); // 7Inherited from
Section titled “Inherited from”Omit.geometric
getState
Section titled “getState”getState: () =>
EngineState
Defined in: src/global/index.ts:5
A JSON-serialisable snapshot of the global stream.
Returns
Section titled “Returns”Example
Section titled “Example”seed(1);const saved = getState();const first = random();setState(saved);random() === first; // trueInherited from
Section titled “Inherited from”Omit.getState
graphemes
Section titled “graphemes”graphemes: (
value,locale?) =>string[]
Defined in: src/namespace/index.ts:46
A random string of count grapheme clusters — what a reader calls
“characters”.
A flag or an emoji with a skin-tone modifier is several code points that display as one glyph. chars counts code points and would split them; this counts what you see.
Parameters
Section titled “Parameters”string
locale?
Section titled “locale?”string
Returns
Section titled “Returns”string[]
Example
Section titled “Example”graphemes(3); // three visible characters, whatever they cost in code pointsInherited from
Section titled “Inherited from”Omit.graphemes
gumbel
Section titled “gumbel”gumbel: (
location,scale) =>number
Defined in: src/global/distributions.ts:161
One Gumbel draw: the distribution of a maximum.
Parameters
Section titled “Parameters”location?
Section titled “location?”number = 0
scale?
Section titled “scale?”number = 1
Returns
Section titled “Returns”number
Example
Section titled “Example”gumbel(); // 0.4759...Inherited from
Section titled “Inherited from”Omit.gumbel
hashFloat
Section titled “hashFloat”hashFloat: (
key,salt?) =>number
Defined in: src/hash.ts:39
A stable double in [0, 1) for key.
Parameters
Section titled “Parameters”Returns
Section titled “Returns”number
Example
Section titled “Example”hashFloat("user-42"); // 0.6031... always this valuehashFloat("user-42", "experiment-b"); // 0.1187... a different salt, a different answerInherited from
Section titled “Inherited from”Omit.hashFloat
hashInteger
Section titled “hashInteger”hashInteger: (
key,min,max,salt?) =>number
Defined in: src/hash.ts:51
A stable integer in [min, max] for key.
Parameters
Section titled “Parameters”number
number
Returns
Section titled “Returns”number
Example
Section titled “Example”hashInteger("user-42", 1, 6); // 3, on every machine and every runInherited from
Section titled “Inherited from”Omit.hashInteger
hashPick
Section titled “hashPick”hashPick: <
T>(key,items,salt?) =>T
Defined in: src/hash.ts:68
A stable choice from items for key.
Type Parameters
Section titled “Type Parameters”T
Parameters
Section titled “Parameters”Collection<T>
Returns
Section titled “Returns”T
Example
Section titled “Example”hashPick("user-42", ["control", "blue", "green"]); // "blue"Inherited from
Section titled “Inherited from”Omit.hashPick
hashRandom
Section titled “hashRandom”hashRandom: (
key,salt?) =>Random
Defined in: src/hash.ts:111
A whole Random seeded from key, when one value is not enough.
Parameters
Section titled “Parameters”Returns
Section titled “Returns”Example
Section titled “Example”const rng = hashRandom("user-42");rng.integer(1, 6); // 5rng.pick(["red", "blue"]); // "blue"// Same key, same sequence, in any process.Inherited from
Section titled “Inherited from”Omit.hashRandom
hex: (
length) =>string
Defined in: src/global/strings.ts:31
A random lowercase hexadecimal string.
Parameters
Section titled “Parameters”length
Section titled “length”number
Returns
Section titled “Returns”string
Example
Section titled “Example”hex(32); // "3f7a1c05e2b8..."Inherited from
Section titled “Inherited from”Omit.hex
hsl: (
options) => [number,number,number,number]
Defined in: src/color.ts:68
[hue, saturation, lightness, alpha], with hue in degrees.
Alpha is 1 unless you ask for it, so the tuple is always four long.
Parameters
Section titled “Parameters”options?
Section titled “options?”ColorOptions = {}
Returns
Section titled “Returns”[number, number, number, number]
Example
Section titled “Example”hsl(); // [ 214, 0.62, 0.55, 1 ]hsl({ hue: [200, 240] }); // blues onlyhsl({ saturation: 0.8, lightness: [0.4, 0.6] });Inherited from
Section titled “Inherited from”Omit.hsl
hypergeometric
Section titled “hypergeometric”hypergeometric: (
population,successes,draws) =>number
Defined in: src/global/distributions.ts:350
Successes when drawing without replacement from a finite pool.
Parameters
Section titled “Parameters”population
Section titled “population”number
successes
Section titled “successes”number
number
Returns
Section titled “Returns”number
Example
Section titled “Example”hypergeometric(50, 5, 10); // 1, drawing 10 from 50 that hold 5Inherited from
Section titled “Inherited from”Omit.hypergeometric
inCircle
Section titled “inCircle”inCircle: (
radius) =>Point
Defined in: src/geometry.ts:77
A uniform point inside a disc.
The square root matters: without it, points bunch up at the centre, because
the area at radius r grows with r.
Parameters
Section titled “Parameters”radius?
Section titled “radius?”number = 1
Returns
Section titled “Returns”Example
Section titled “Example”inCircle(50); // [ 12.4, -31.8 ] evenly spread over the discInherited from
Section titled “Inherited from”Omit.inCircle
inRect
Section titled “inRect”inRect: (
rect) =>Point
Defined in: src/geometry.ts:124
A uniform point inside a rectangle.
Parameters
Section titled “Parameters”Returns
Section titled “Returns”Example
Section titled “Example”inRect({ x: 0, y: 0, width: 1920, height: 1080 }); // [ 842.1, 219.7 ]Inherited from
Section titled “Inherited from”Omit.inRect
inSphere
Section titled “inSphere”inSphere: (
radius,dimensions) =>Point
Defined in: src/geometry.ts:108
A uniform point inside a ball, by volume.
Parameters
Section titled “Parameters”radius?
Section titled “radius?”number = 1
dimensions?
Section titled “dimensions?”number = 3
Returns
Section titled “Returns”Example
Section titled “Example”inSphere(10); // [ 3.1, -5.7, 1.2 ]Inherited from
Section titled “Inherited from”Omit.inSphere
integer
Section titled “integer”integer: (
min,max) =>number
Defined in: src/global/numbers.ts:39
An integer in [min, max], both ends included.
Parameters
Section titled “Parameters”number
number
Returns
Section titled “Returns”number
Example
Section titled “Example”integer(1, 6); // 4integer(-10, 10); // -3Inherited from
Section titled “Inherited from”Omit.integer
integers
Section titled “integers”integers: (
n,min,max) =>Float64Array
Defined in: src/global/numbers.ts:200
n integers in [min, max], with the bounds validated once.
Parameters
Section titled “Parameters”number
number
number
Returns
Section titled “Returns”Float64Array
Example
Section titled “Example”integers(1_000, 1, 6); // Float64Array(1000) [ 3, 5, 3, 1, ... ]Inherited from
Section titled “Inherited from”Omit.integers
irwinHall
Section titled “irwinHall”irwinHall: (
n) =>number
Defined in: src/global/distributions.ts:223
One draw: the sum of n uniforms.
Parameters
Section titled “Parameters”number
Returns
Section titled “Returns”number
Example
Section titled “Example”irwinHall(12); // 6.1284... a cheap normal approximationInherited from
Section titled “Inherited from”Omit.irwinHall
jitter
Section titled “jitter”jitter: (
base,factor) =>number
Defined in: src/time.ts:83
Spread a value by +/- factor so that things scheduled together stop
arriving together. jitter(1000, 0.1) lands in [900, 1100).
Parameters
Section titled “Parameters”number
factor?
Section titled “factor?”number = 0.1
Returns
Section titled “Returns”number
Example
Section titled “Example”jitter(1_000); // 964.27... within 10%jitter(1_000, 0.5); // 1387.51... within 50%Inherited from
Section titled “Inherited from”Omit.jitter
laplace
Section titled “laplace”laplace: (
location,scale) =>number
Defined in: src/global/distributions.ts:137
One Laplace draw: two exponential tails back to back.
Parameters
Section titled “Parameters”location?
Section titled “location?”number = 0
scale?
Section titled “scale?”number = 1
Returns
Section titled “Returns”number
Example
Section titled “Example”laplace(0, 1); // 0.3819...Inherited from
Section titled “Inherited from”Omit.laplace
logistic
Section titled “logistic”logistic: (
location,scale) =>number
Defined in: src/global/distributions.ts:149
One logistic draw, the distribution behind the sigmoid.
Parameters
Section titled “Parameters”location?
Section titled “location?”number = 0
scale?
Section titled “scale?”number = 1
Returns
Section titled “Returns”number
Example
Section titled “Example”logistic(); // -0.2231...Inherited from
Section titled “Inherited from”Omit.logistic
logNormal
Section titled “logNormal”logNormal: (
mu,sigma) =>number
Defined in: src/global/distributions.ts:65
One log-normal draw: a normal variable exponentiated.
Parameters
Section titled “Parameters”number = 0
sigma?
Section titled “sigma?”number = 1
Returns
Section titled “Returns”number
Example
Section titled “Example”logNormal(); // 1.6221...logNormal(0, 0.5); // 1.2840...Inherited from
Section titled “Inherited from”Omit.logNormal
multinomial
Section titled “multinomial”multinomial: (
n,weights) =>number[]
Defined in: src/global/distributions.ts:380
Counts per category, summing to n.
Parameters
Section titled “Parameters”number
weights
Section titled “weights”ArrayLike<number>
Returns
Section titled “Returns”number[]
Example
Section titled “Example”multinomial(100, [1, 3, 6]); // [ 9, 31, 60 ]Inherited from
Section titled “Inherited from”Omit.multinomial
nanoid
Section titled “nanoid”nanoid: (
size,options) =>string
Defined in: src/namespace/index.ts:40
A nanoid: 21 URL-safe characters by default. Drop-in for the nanoid package.
Parameters
Section titled “Parameters”number = 21
options?
Section titled “options?”NanoidOptions = {}
Returns
Section titled “Returns”string
Example
Section titled “Example”nanoid(); // "V1StGXR8_Z5jdHi6B-myT"nanoid(10); // "IRFa-VaY2b"nanoid(12, { alphabet: "0123456789abcdef" }); // "3f7a1c05e2b8"Inherited from
Section titled “Inherited from”Omit.nanoid
negativeBinomial
Section titled “negativeBinomial”negativeBinomial: (
r,p) =>number
Defined in: src/global/distributions.ts:338
The number of failures before the r-th success.
Parameters
Section titled “Parameters”number
number
Returns
Section titled “Returns”number
Example
Section titled “Example”negativeBinomial(3, 0.5); // 4Inherited from
Section titled “Inherited from”Omit.negativeBinomial
normal
Section titled “normal”normal: (
mean,sd) =>number
Defined in: src/global/distributions.ts:24
One Gaussian draw.
Parameters
Section titled “Parameters”number = 0
number = 1
Returns
Section titled “Returns”number
Example
Section titled “Example”normal(); // 0.4837... mean 0, sd 1normal(100, 15); // 92.61... an IQ-shaped drawInherited from
Section titled “Inherited from”Omit.normal
onCircle
Section titled “onCircle”onCircle: (
radius) =>Point
Defined in: src/geometry.ts:60
A uniform point on the circumference of a circle.
Parameters
Section titled “Parameters”radius?
Section titled “radius?”number = 1
Returns
Section titled “Returns”Example
Section titled “Example”onCircle(100); // [ -70.7, 70.7 ] always exactly 100 from the originInherited from
Section titled “Inherited from”Omit.onCircle
oneIn: (
n) =>boolean
Defined in: src/global/numbers.ts:114
true with probability 1 / n.
Parameters
Section titled “Parameters”number
Returns
Section titled “Returns”boolean
Example
Section titled “Example”oneIn(20); // a critical hit on a d20Inherited from
Section titled “Inherited from”Omit.oneIn
onSphere
Section titled “onSphere”onSphere: (
radius,dimensions) =>Point
Defined in: src/geometry.ts:94
A uniform point on the surface of a sphere.
Parameters
Section titled “Parameters”radius?
Section titled “radius?”number = 1
dimensions?
Section titled “dimensions?”number = 3
Returns
Section titled “Returns”Example
Section titled “Example”onSphere(1); // [ 0.267, -0.534, 0.801 ]onSphere(2, 4); // a point on a 4-dimensional sphere of radius 2Inherited from
Section titled “Inherited from”Omit.onSphere
otp: (
digits,options) =>string
Defined in: src/namespace/index.ts:43
A numeric one-time code. Leading zeros are preserved.
Parameters
Section titled “Parameters”digits?
Section titled “digits?”number = 6
options?
Section titled “options?”TokenOptions = {}
Returns
Section titled “Returns”string
Example
Section titled “Example”otp(); // "047215" six digits, leading zero keptotp(8); // "90114862"Inherited from
Section titled “Inherited from”Omit.otp
pareto
Section titled “pareto”pareto: (
shape,scale) =>number
Defined in: src/global/distributions.ts:173
One Pareto draw: the heavy-tailed shape behind the 80/20 rule.
Parameters
Section titled “Parameters”number
scale?
Section titled “scale?”number = 1
Returns
Section titled “Returns”number
Example
Section titled “Example”pareto(1.16); // 1.8721... scale 1 by defaultInherited from
Section titled “Inherited from”Omit.pareto
password
Section titled “password”password: (
length,options) =>string
Defined in: src/namespace/index.ts:44
A random password. With requireEach the result holds at least one character
from every enabled class, then gets shuffled so their positions are random.
Parameters
Section titled “Parameters”length?
Section titled “length?”number = 16
options?
Section titled “options?”PasswordOptions = {}
Returns
Section titled “Returns”string
Example
Section titled “Example”password(); // "vK8mQ2xL9pT4nR7c"password(24, { symbols: true, requireEach: true });password(12, { uppercase: false, symbols: false });Inherited from
Section titled “Inherited from”Omit.password
pastDate
Section titled “pastDate”pastDate: (
days) =>Date
Defined in: src/time.ts:40
A Date within the last days (default 7).
Parameters
Section titled “Parameters”number = 7
Returns
Section titled “Returns”Date
Example
Section titled “Example”pastDate(); // some time in the last weekpastDate(365); // some time in the last yearInherited from
Section titled “Inherited from”Omit.pastDate
permutation
Section titled “permutation”permutation: (
n) =>number[]
Defined in: src/global/collections.ts:172
A shuffled [0, n), for permuting something you index yourself.
Parameters
Section titled “Parameters”number
Returns
Section titled “Returns”number[]
Example
Section titled “Example”permutation(5); // [ 3, 0, 4, 1, 2 ]Inherited from
Section titled “Inherited from”Omit.permutation
pick: <
T>(items) =>T
Defined in: src/global/collections.ts:15
One element. Throws when the collection is empty.
Type Parameters
Section titled “Type Parameters”T
Parameters
Section titled “Parameters”Collection<T>
Returns
Section titled “Returns”T
Example
Section titled “Example”pick(["rock", "paper", "scissors"]); // "paper"pick("abcdef"); // "d"pick(new Set([1, 2, 3])); // 2Inherited from
Section titled “Inherited from”Omit.pick
pickEntry
Section titled “pickEntry”pickEntry: <
K,V>(target) => [K,V]
Defined in: src/global/collections.ts:66
One [key, value] pair of a plain object or Map.
Type Parameters
Section titled “Type Parameters”K extends string | number | symbol
V
Parameters
Section titled “Parameters”target
Section titled “target”Record<K, V> | Map<K, V>
Returns
Section titled “Returns”[K, V]
Example
Section titled “Example”pickEntry({ a: 1, b: 2 }); // [ "b", 2 ]Inherited from
Section titled “Inherited from”Omit.pickEntry
pickIndex
Section titled “pickIndex”pickIndex: <
T>(items) =>number
Defined in: src/global/collections.ts:40
The index of one element, rather than the element itself.
Type Parameters
Section titled “Type Parameters”T
Parameters
Section titled “Parameters”Collection<T>
Returns
Section titled “Returns”number
Example
Section titled “Example”pickIndex(["a", "b", "c"]); // 2Inherited from
Section titled “Inherited from”Omit.pickIndex
pickKey
Section titled “pickKey”pickKey: <
K,V>(target) =>K
Defined in: src/global/collections.ts:52
One key of a plain object or Map.
Type Parameters
Section titled “Type Parameters”K extends string | number | symbol
V
Parameters
Section titled “Parameters”target
Section titled “target”Record<K, V> | Map<K, V>
Returns
Section titled “Returns”K
Example
Section titled “Example”pickKey({ small: 1, medium: 2, large: 3 }); // "medium"Inherited from
Section titled “Inherited from”Omit.pickKey
pickValue
Section titled “pickValue”pickValue: <
K,V>(target) =>V
Defined in: src/global/collections.ts:249
A uniformly chosen value of a plain object or Map.
Type Parameters
Section titled “Type Parameters”K extends string | number | symbol
V
Parameters
Section titled “Parameters”target
Section titled “target”Record<K, V> | Map<K, V>
Returns
Section titled “Returns”V
Example
Section titled “Example”pickValue({ a: 1, b: 2, c: 3 }); // 3Inherited from
Section titled “Inherited from”Omit.pickValue
poisson
Section titled “poisson”poisson: (
lambda) =>number
Defined in: src/global/distributions.ts:313
The number of events in one interval, given a mean rate.
Parameters
Section titled “Parameters”lambda
Section titled “lambda”number
Returns
Section titled “Returns”number
Example
Section titled “Example”poisson(4); // 3Inherited from
Section titled “Inherited from”Omit.poisson
random
Section titled “random”random: () =>
number
Defined in: src/global/numbers.ts:12
A double in [0, 1). The Math.random drop-in.
Returns
Section titled “Returns”number
Example
Section titled “Example”random(); // 0.7401962...Inherited from
Section titled “Inherited from”Omit.random
Random
Section titled “Random”Random: typeof
Random
Defined in: src/namespace/index.ts:54
Inherited from
Section titled “Inherited from”Omit.Random
range: (
start,stop?,step?) =>number
Defined in: src/global/numbers.ts:66
Python’s randrange: a member of [start, stop) stepping by step.
Parameters
Section titled “Parameters”number
number
number
Returns
Section titled “Returns”number
Example
Section titled “Example”range(10); // 7 one argument is the upper boundrange(5, 10); // 8range(0, 100, 5); // 45 multiples of five onlyInherited from
Section titled “Inherited from”Omit.range
RansuError
Section titled “RansuError”RansuError: typeof
RansuError
Defined in: src/namespace/index.ts:56
Inherited from
Section titled “Inherited from”Omit.RansuError
rayleigh
Section titled “rayleigh”rayleigh: (
scale) =>number
Defined in: src/global/distributions.ts:198
One Rayleigh draw: the length of a 2D Gaussian vector.
Parameters
Section titled “Parameters”scale?
Section titled “scale?”number = 1
Returns
Section titled “Returns”number
Example
Section titled “Example”rayleigh(); // 1.1774...Inherited from
Section titled “Inherited from”Omit.rayleigh
reservoir
Section titled “reservoir”reservoir: <
T>(items,k) =>T[]
Defined in: src/global/collections.ts:122
k elements from an iterable of unknown length, in one pass.
Type Parameters
Section titled “Type Parameters”T
Parameters
Section titled “Parameters”Iterable<T>
number
Returns
Section titled “Returns”T[]
Example
Section titled “Example”function* lines() { yield "first"; yield "second"; yield "third";}// Nothing is buffered, so the source may be far larger than memory.reservoir(lines(), 2); // [ "third", "first" ]Inherited from
Section titled “Inherited from”Omit.reservoir
rgb: (
options) => [number,number,number,number]
Defined in: src/color.ts:115
[red, green, blue, alpha], channels 0 to 255 and alpha 0 to 1.
Parameters
Section titled “Parameters”options?
Section titled “options?”ColorOptions = {}
Returns
Section titled “Returns”[number, number, number, number]
Example
Section titled “Example”rgb(); // [ 62, 129, 214, 1 ]rgb({ alpha: [0.2, 0.8] }); // [ 200, 41, 96, 0.57 ]Inherited from
Section titled “Inherited from”Omit.rgb
rollout
Section titled “rollout”rollout: (
key,percent,salt?) =>boolean
Defined in: src/hash.ts:95
Whether key falls inside a percent rollout, where percent is a fraction
in [0, 1]. Growing the percentage only ever adds keys, never moves one out.
Parameters
Section titled “Parameters”percent
Section titled “percent”number
Returns
Section titled “Returns”boolean
Example
Section titled “Example”rollout("user-42", 0.1); // false, not in the first 10%rollout("user-42", 0.5); // true, and it stays true as the rollout widensInherited from
Section titled “Inherited from”Omit.rollout
sample
Section titled “sample”sample: <
T>(items,k) =>T[]
Defined in: src/global/collections.ts:92
k distinct elements, in random order.
Type Parameters
Section titled “Type Parameters”T
Parameters
Section titled “Parameters”Collection<T>
number
Returns
Section titled “Returns”T[]
Example
Section titled “Example”sample([1, 2, 3, 4, 5], 3); // [ 4, 1, 5 ]Inherited from
Section titled “Inherited from”Omit.sample
sampleIntegers
Section titled “sampleIntegers”sampleIntegers: (
count,min,max) =>number[]
Defined in: src/global/numbers.ts:227
count distinct integers in [min, max], without materialising the range.
Parameters
Section titled “Parameters”number
number
number
Returns
Section titled “Returns”number[]
Example
Section titled “Example”sampleIntegers(6, 1, 49); // [ 12, 3, 41, 28, 7, 33 ]sampleIntegers(3, 0, 1_000_000_000); // fine: the range is never builtInherited from
Section titled “Inherited from”Omit.sampleIntegers
seed: (
value) =>void
Defined in: src/global/index.ts:5
Make every top-level function deterministic from here on.
Parameters
Section titled “Parameters”Returns
Section titled “Returns”void
Example
Section titled “Example”seed(42);integer(1, 6); // 3seed(42);integer(1, 6); // 3 againInherited from
Section titled “Inherited from”Omit.seed
SeedSequence
Section titled “SeedSequence”SeedSequence: typeof
SeedSequence
Defined in: src/namespace/index.ts:55
Inherited from
Section titled “Inherited from”Omit.SeedSequence
setState
Section titled “setState”setState: (
state) =>void
Defined in: src/global/index.ts:5
Rewind or fast-forward the global stream to a saved snapshot.
Parameters
Section titled “Parameters”Returns
Section titled “Returns”void
Example
Section titled “Example”seed(1);setState(getState()); // a no-op, but this is the shapeInherited from
Section titled “Inherited from”Omit.setState
shuffle
Section titled “shuffle”shuffle: <
T>(items) =>T[]
Defined in: src/global/collections.ts:147
A shuffled copy. The input is untouched.
Type Parameters
Section titled “Type Parameters”T
Parameters
Section titled “Parameters”Collection<T>
Returns
Section titled “Returns”T[]
Example
Section titled “Example”shuffle([1, 2, 3, 4, 5]); // [ 3, 1, 5, 2, 4 ]Inherited from
Section titled “Inherited from”Omit.shuffle
shuffleInPlace
Section titled “shuffleInPlace”shuffleInPlace: <
T>(items) =>T[]
Defined in: src/global/collections.ts:160
Fisher–Yates in place: the only mutating shuffle.
Type Parameters
Section titled “Type Parameters”T
Parameters
Section titled “Parameters”T[]
Returns
Section titled “Returns”T[]
Example
Section titled “Example”const deck = [1, 2, 3, 4, 5];shuffleInPlace(deck); // deck itself is reordered and returnedInherited from
Section titled “Inherited from”Omit.shuffleInPlace
shuffleString
Section titled “shuffleString”shuffleString: (
value) =>string
Defined in: src/global/collections.ts:184
The characters of a string in random order, by code point.
Parameters
Section titled “Parameters”string
Returns
Section titled “Returns”string
Example
Section titled “Example”shuffleString("ransu"); // "nusar"Inherited from
Section titled “Inherited from”Omit.shuffleString
sign: () =>
number
Defined in: src/global/numbers.ts:126
-1 or 1.
Returns
Section titled “Returns”number
Example
Section titled “Example”sign() * 5; // -5Inherited from
Section titled “Inherited from”Omit.sign
stream
Section titled “stream”stream: () =>
Generator<number,never,unknown>
Defined in: src/global/numbers.ts:214
An endless stream of doubles in [0, 1).
Returns
Section titled “Returns”Generator<number, never, unknown>
Example
Section titled “Example”for (const value of stream()) { if (value > 0.99) break;}Inherited from
Section titled “Inherited from”Omit.stream
string
Section titled “string”string: (
length,alphabet?) =>string
Defined in: src/global/strings.ts:16
A random string over alphabet (default alphanumeric).
Parameters
Section titled “Parameters”length
Section titled “length”number
alphabet?
Section titled “alphabet?”string | ArrayLike<string>
Returns
Section titled “Returns”string
Example
Section titled “Example”string(12); // "yYwec14FcPKc"string(6, "ABCDEF0123456789"); // "3E9A1C"string(4, ["cat", "dog"]); // "dogcatdogdog"Inherited from
Section titled “Inherited from”Omit.string
studentT
Section titled “studentT”studentT: (
df) =>number
Defined in: src/global/distributions.ts:101
One Student’s t draw: a normal with heavier tails.
Parameters
Section titled “Parameters”number
Returns
Section titled “Returns”number
Example
Section titled “Example”studentT(5); // -0.8412...Inherited from
Section titled “Inherited from”Omit.studentT
subset
Section titled “subset”subset: <
T>(items,p) =>T[]
Defined in: src/global/collections.ts:266
Each element kept independently with probability p.
The result has no fixed length: it is a coin flip per element, so the size
varies around items.length * p. Order is preserved.
Type Parameters
Section titled “Type Parameters”T
Parameters
Section titled “Parameters”Collection<T>
number
Returns
Section titled “Returns”T[]
Example
Section titled “Example”subset([1, 2, 3, 4, 5, 6], 0.5); // [ 1, 4, 5 ]Inherited from
Section titled “Inherited from”Omit.subset
takeOut
Section titled “takeOut”takeOut: <
T>(items) =>T
Defined in: src/global/collections.ts:135
Remove one random element and return it. Mutates the array.
Type Parameters
Section titled “Type Parameters”T
Parameters
Section titled “Parameters”T[]
Returns
Section titled “Returns”T
Example
Section titled “Example”const deck = ["A", "K", "Q", "J"];takeOut(deck); // "Q", and deck is now [ "A", "K", "J" ]Inherited from
Section titled “Inherited from”Omit.takeOut
token: (
bytes,options) =>string
Defined in: src/namespace/index.ts:42
A URL-safe secret token with bytes bytes of entropy. CSPRNG-backed.
Parameters
Section titled “Parameters”bytes?
Section titled “bytes?”number = 32
options?
Section titled “options?”TokenOptions = {}
Returns
Section titled “Returns”string
Example
Section titled “Example”token(); // "xK3n_9QpZ..." 32 bytes as base64urltoken(16); // shorter, still from the platform CSPRNGInherited from
Section titled “Inherited from”Omit.token
triangular
Section titled “triangular”triangular: (
min,max,mode?) =>number
Defined in: src/global/distributions.ts:211
One draw from a triangle, with an optional peak.
Parameters
Section titled “Parameters”number
number
number
Returns
Section titled “Returns”number
Example
Section titled “Example”triangular(0, 10); // 4.7213... peak at the midpointtriangular(0, 10, 9); // 7.8102... peak at 9Inherited from
Section titled “Inherited from”Omit.triangular
tryPick
Section titled “tryPick”tryPick: <
T>(items) =>T|undefined
Defined in: src/global/collections.ts:28
One element, or undefined when the collection is empty.
Type Parameters
Section titled “Type Parameters”T
Parameters
Section titled “Parameters”Collection<T>
Returns
Section titled “Returns”T | undefined
Example
Section titled “Example”tryPick([]); // undefinedtryPick(["a", "b"]); // "a"Inherited from
Section titled “Inherited from”Omit.tryPick
ulid:
UlidApi
Defined in: src/namespace/index.ts:41
A ULID: 26 characters of Crockford base32, time-ordered.
48 bits of millisecond timestamp then 80 bits of randomness, encoded so that lexicographic order is time order. Shorter than a UUID and case-insensitive, with no dashes to strip.
Example
Section titled “Example”const id = ulid(); // "01K39XQZP4W8YHN2VBTKD7A3RM"ulid.timestamp(id); // 1756890764019
// Sorts in creation order as a plain string.ulid() < ulid(); // trueInherited from
Section titled “Inherited from”Omit.ulid
unicodeRanges
Section titled “unicodeRanges”unicodeRanges:
object
Defined in: src/namespace/index.ts:47
The named Unicode blocks the string functions accept.
readonlyall: readonly [readonly [0,1114111]]
arabic
Section titled “arabic”
readonlyarabic: readonly [readonly [1536,1791]]
arrows
Section titled “arrows”
readonlyarrows: readonlyCodePointRange[] =ARROWS
readonlyascii: readonlyCodePointRange[] =ASCII
blockElements
Section titled “blockElements”
readonlyblockElements: readonlyCodePointRange[] =BLOCKS
readonlybmp: readonly [readonly [0,65535]]
readonlybox: readonlyCodePointRange[] =BOX
braille
Section titled “braille”
readonlybraille: readonlyCodePointRange[] =BRAILLE
readonlycjk: readonlyCodePointRange[]
currency
Section titled “currency”
readonlycurrency: readonlyCodePointRange[] =CURRENCY
cyrillic
Section titled “cyrillic”
readonlycyrillic: readonlyCodePointRange[] =CYRILLIC
devanagari
Section titled “devanagari”
readonlydevanagari: readonly [readonly [2304,2431]]
readonlyemoji: readonlyCodePointRange[] =EMOJI
geometric
Section titled “geometric”
readonlygeometric: readonlyCodePointRange[] =GEOMETRIC
readonlygreek: readonlyCodePointRange[] =GREEK
hangul
Section titled “hangul”
readonlyhangul: readonlyCodePointRange[] =HANGUL
hebrew
Section titled “hebrew”
readonlyhebrew: readonly [readonly [1424,1535]]
hiragana
Section titled “hiragana”
readonlyhiragana: readonlyCodePointRange[] =HIRAGANA
readonlykana: readonlyCodePointRange[]
readonlykanji: readonlyCodePointRange[] =KANJI
katakana
Section titled “katakana”
readonlykatakana: readonlyCodePointRange[] =KATAKANA
readonlylatin: readonlyCodePointRange[] =LATIN
latin1
Section titled “latin1”
readonlylatin1: readonly [readonly [32,126], readonly [160,255]]
latinExtended
Section titled “latinExtended”
readonlylatinExtended: readonly [readonly [256,591]]
readonlymath: readonlyCodePointRange[] =MATH
printable
Section titled “printable”
readonlyprintable: readonlyCodePointRange[]
punctuation
Section titled “punctuation”
readonlypunctuation: readonlyCodePointRange[] =PUNCTUATION
symbols
Section titled “symbols”
readonlysymbols: readonlyCodePointRange[]
readonlythai: readonly [readonly [3584,3711]]
Example
Section titled “Example”import { char, unicodeRanges } from "ransu";
Object.keys(unicodeRanges); // "printable", "latin", "hiragana", ...
char({ blocks: "hiragana" });char({ blocks: ["hiragana", "katakana"] });Inherited from
Section titled “Inherited from”Omit.unicodeRanges
unitVector
Section titled “unitVector”unitVector: (
dimensions) =>Point
Defined in: src/geometry.ts:30
A uniform direction in dimensions dimensions, as a unit vector.
Normalised Gaussians, which is the only construction that stays uniform on the sphere in every dimension. Picking each coordinate uniformly and normalising concentrates points toward the corners of the cube.
Parameters
Section titled “Parameters”dimensions?
Section titled “dimensions?”number = 2
Returns
Section titled “Returns”Example
Section titled “Example”unitVector(); // [ -0.6018, 0.7986 ] length 1unitVector(3); // [ 0.2673, -0.5345, 0.8018 ]Inherited from
Section titled “Inherited from”Omit.unitVector
uuid:
UuidApi
Defined in: src/namespace/index.ts:39
A UUID, in every version RFC 9562 defines.
Calling it gives a v4; every version and helper hangs off it. Names and
argument order match the uuid package, so it is a drop-in for that.
Example
Section titled “Example”uuid(); // "39817b65-27b9-45d0-9b51-315d530a3211" a v4uuid.v4(); // the same thing, spelled out
// Time-ordered, so it sorts by creation time in a database index.uuid.v7(); // "0198f2a4-1c05-7e2b-8f31-9d4a7c0e15b3"
// Name-based: the same name always gives the same UUID.uuid.v5("example.com", uuid.NAMESPACE.DNS);
uuid.validate(uuid()); // trueuuid.version(uuid.v7()); // 7uuid.timestamp(uuid.v7()); // 1756890764019
uuid.NIL; // "00000000-0000-0000-0000-000000000000"uuid.MAX; // "ffffffff-ffff-ffff-ffff-ffffffffffff"Inherited from
Section titled “Inherited from”Omit.uuid
vonMises
Section titled “vonMises”vonMises: (
mu,kappa) =>number
Defined in: src/global/distributions.ts:249
One angle in (-pi, pi]. The circular analogue of a normal.
Parameters
Section titled “Parameters”number = 0
kappa?
Section titled “kappa?”number = 1
Returns
Section titled “Returns”number
Example
Section titled “Example”vonMises(); // 0.3218...vonMises(0, 10); // 0.0782... a larger kappa concentrates near muInherited from
Section titled “Inherited from”Omit.vonMises
weibull
Section titled “weibull”weibull: (
shape,scale) =>number
Defined in: src/global/distributions.ts:186
One Weibull draw, used for time until failure.
Parameters
Section titled “Parameters”number
scale?
Section titled “scale?”number = 1
Returns
Section titled “Returns”number
Example
Section titled “Example”weibull(1.5); // 0.7943...weibull(1.5, 100); // 79.43...Inherited from
Section titled “Inherited from”Omit.weibull
weightedPick
Section titled “weightedPick”weightedPick: <
T>(items,weights) =>T
Defined in: src/global/collections.ts:197
One element, with probability proportional to its weight.
Type Parameters
Section titled “Type Parameters”T
Parameters
Section titled “Parameters”Collection<T>
weights
Section titled “weights”ArrayLike<number>
Returns
Section titled “Returns”T
Example
Section titled “Example”// "common" ten times as often as "rare".weightedPick(["common", "rare"], [10, 1]); // "common"Inherited from
Section titled “Inherited from”Omit.weightedPick
weightedSample
Section titled “weightedSample”weightedSample: <
T>(items,weights,k) =>T[]
Defined in: src/global/collections.ts:212
k distinct elements, drawn in proportion to their weights.
Type Parameters
Section titled “Type Parameters”T
Parameters
Section titled “Parameters”Collection<T>
weights
Section titled “weights”ArrayLike<number>
number
Returns
Section titled “Returns”T[]
Example
Section titled “Example”weightedSample(["a", "b", "c"], [1, 3, 6], 2); // [ "c", "b" ]Inherited from
Section titled “Inherited from”Omit.weightedSample
weightedTable
Section titled “weightedTable”weightedTable: <
T>(items,weights) =>object
Defined in: src/global/collections.ts:233
A reusable weighted sampler, O(1) per draw.
Build one when the same weights are drawn from repeatedly; weightedPick is a linear scan and is the better choice for a one-off.
Type Parameters
Section titled “Type Parameters”T
Parameters
Section titled “Parameters”Collection<T>
weights
Section titled “weights”ArrayLike<number>
Returns
Section titled “Returns”object
pick()
Section titled “pick()”pick():
T
Returns
Section titled “Returns”T
Example
Section titled “Example”const loot = weightedTable(["common", "rare", "epic"], [90, 9, 1]);loot.pick(); // "common"loot.pick(); // "common"Inherited from
Section titled “Inherited from”Omit.weightedTable
zipf: (
s,n) =>number
Defined in: src/global/distributions.ts:367
A rank in 1..n, following the power law behind word frequency.
Parameters
Section titled “Parameters”number
number
Returns
Section titled “Returns”number
Example
Section titled “Example”zipf(1.2, 1000); // 3Inherited from
Section titled “Inherited from”Omit.zipf
Methods
Section titled “Methods”clone()
Section titled “clone()”clone():
Random
Defined in: src/namespace/index.ts:61
An independent copy of the global stream, positioned where it is now.
Returns
Section titled “Returns”Inherited from
Section titled “Inherited from”Omit.clone
split()
Section titled “split()”split(
n):Random[]
Defined in: src/namespace/index.ts:73
n independent generators derived from the global stream.
Parameters
Section titled “Parameters”number
Returns
Section titled “Returns”Random[]
Inherited from
Section titled “Inherited from”Omit.split