Wondering what’s next for npm?Check out our public roadmap! »

@thi.ng/random
TypeScript icon, indicating that this package has built-in type declarations

2.3.1 • Public • Published

random

npm version npm downloads Twitter Follow

This project is part of the @thi.ng/umbrella monorepo.

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

Status

STABLE - used in production

Search or submit any issues for this package

Related packages

  • @thi.ng/ksuid - Configurable K-sortable unique identifiers, binary & base-N encoded

Installation

yarn add @thi.ng/random
// ES module
<script type="module" src="https://unpkg.com/@thi.ng/random?module" crossorigin></script>

// UMD
<script src="https://unpkg.com/@thi.ng/random/lib/index.umd.js" crossorigin></script>

Package sizes (gzipped, pre-treeshake): ESM: 1.66 KB / CJS: 1.79 KB / UMD: 1.77 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
Interactive pixel sorting tool using thi.ng/color & thi.ng/pixel Demo Source
Evolutionary shader generation using genetic programming Demo Source

API

Generated API docs

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

// next gaussian (using iterative CLT approach)
// optional params: num samples, offset, scale
rnd.gaussian()
// 0.10632886109089679

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 - 2021 Karsten Schmidt // Apache Software License 2.0

Install

npm i @thi.ng/random

DownloadsWeekly Downloads

4,313

Version

2.3.1

License

Apache-2.0

Unpacked Size

93 kB

Total Files

50

Last publish

Collaborators

  • avatar