Package Exports
- @thi.ng/random
- @thi.ng/random/api
- @thi.ng/random/arandom
- @thi.ng/random/coin
- @thi.ng/random/constants
- @thi.ng/random/crypto
- @thi.ng/random/distributions/exponential
- @thi.ng/random/distributions/gaussian
- @thi.ng/random/distributions/geometric
- @thi.ng/random/distributions/normal
- @thi.ng/random/distributions/uniform
- @thi.ng/random/pick-random
- @thi.ng/random/random-bytes
- @thi.ng/random/random-id
- @thi.ng/random/sfc32
- @thi.ng/random/smush32
- @thi.ng/random/system
- @thi.ng/random/unique-indices
- @thi.ng/random/uuid
- @thi.ng/random/weighted-random
- @thi.ng/random/xorshift128
- @thi.ng/random/xorwow
- @thi.ng/random/xoshiro128
- @thi.ng/random/xsadd
Readme
This project is part of the @thi.ng/umbrella monorepo.
- About
- Status
- Support packages
- Related packages
- Installation
- Dependencies
- Usage examples
- API
- Authors
- License
About
Pseudo-random number generators w/ unified API, distributions, weighted choices, ID generation.
This package provides the IRandom
interface and various (mostly seedable)
pseudo-random number generator implementations, incl. IRandom
wrappers for
Math.random()
and window.crypto
:
Partially ported from C implementations taken from c.thi.ng.
Random distributions
Other utilities
coin()
/fairCoin()
pickRandom()
/pickRandomKey()
randomBytes()
/randomBytesFrom()
randomID()
weightedRandom()
/weightedRandomKey()
uniqueIndices()
/uniqueValuesFrom()
uuidv4Bytes()
/uuid()
Status
STABLE - used in production
Search or submit any issues for this package
Support packages
- @thi.ng/random-fxhash - @thi.ng/random compatible wrapper & utilities for fxhash's PRNG
Related packages
- @thi.ng/ksuid - Configurable K-sortable unique IDs, ULIDs, binary & base-N encoded, 32/48/64bit time resolutions
Installation
yarn add @thi.ng/random
ES module import:
<script type="module" src="https://cdn.skypack.dev/@thi.ng/random"></script>
For Node.js REPL:
# with flag only for < v16
node --experimental-repl-await
> const random = await import("@thi.ng/random");
Package sizes (brotli'd, pre-treeshake): ESM: 1.83 KB
Dependencies
Usage examples
Several demos in this repo's /examples directory are using this package.
A selection:
Screenshot | Description | Live demo | Source |
---|---|---|---|
![]() |
Probabilistic color theme generator | Demo | Source |
![]() |
Interactive inverse FFT toy synth | Demo | Source |
![]() |
2D Bezier curve-guided particle system | Demo | Source |
![]() |
Interactive pixel sorting tool using thi.ng/color & thi.ng/pixel | Demo | Source |
![]() |
Evolutionary shader generation using genetic programming | Demo | Source |
API
import { Smush32 } from "@thi.ng/random";
const rnd = new Smush32(0xdecafbad);
// the following methods are available for all generators
// next uint (0 .. 2^32-1)
rnd.int()
// 4022849029
// next float [0.0 .. 1.0)
rnd.float()
// 0.2698542904737066
// next normalized float (w/ opt scale)
// [-scale .. +scale)
rnd.norm(100)
// 57.70723665079737
// next float in given interval [min .. max)
rnd.minmax(10, 20)
// 15.295951807707537
rnd.minmaxInt(10, 20)
Authors
Karsten Schmidt
If this project contributes to an academic publication, please cite it as:
@misc{thing-random,
title = "@thi.ng/random",
author = "Karsten Schmidt",
note = "https://thi.ng/random",
year = 2015
}
License
© 2015 - 2022 Karsten Schmidt // Apache Software License 2.0