What is @thi.ng/random?
@thi.ng/random is a versatile library for generating random numbers and sequences. It provides various random number generators (RNGs) and utilities for creating random values, sequences, and distributions.
What are @thi.ng/random's main functionalities?
Basic Random Number Generation
This feature allows you to generate basic random numbers, both floating-point and integer values, using the default system RNG.
const { SYSTEM } = require('@thi.ng/random');
const rng = SYSTEM;
console.log(rng.float()); // Generates a random float between 0 and 1
console.log(rng.int()); // Generates a random integer
Seeded Random Number Generation
This feature allows you to create a seeded random number generator, which produces reproducible sequences of random numbers.
const { XsAdd } = require('@thi.ng/random');
const rng = new XsAdd(12345); // Seeded RNG
console.log(rng.float()); // Generates a reproducible random float
console.log(rng.int()); // Generates a reproducible random integer
Random Value Generation from Arrays
This feature allows you to randomly select values from an array, which is useful for shuffling or sampling data.
const { SYSTEM } = require('@thi.ng/random');
const rng = SYSTEM;
const arr = [1, 2, 3, 4, 5];
console.log(rng.choice(arr)); // Randomly selects an element from the array
Random Distributions
This feature allows you to generate random numbers from various statistical distributions, such as normal (Gaussian) distributions.
const { SYSTEM } = require('@thi.ng/random');
const rng = SYSTEM;
console.log(rng.norm(0, 1)); // Generates a random number from a normal distribution with mean 0 and standard deviation 1
Other packages similar to @thi.ng/random
random-js
random-js is a library for generating random numbers and values. It provides a variety of RNGs and utilities for creating random values, similar to @thi.ng/random. However, random-js focuses more on providing a simple API for common randomization tasks.
seedrandom
seedrandom is a library for seeded random number generation. It allows you to create reproducible random sequences by providing a seed. While it offers fewer utilities compared to @thi.ng/random, it is highly focused on the quality and reproducibility of random numbers.
chance
chance is a library for generating random data for various purposes, including numbers, strings, and even entire objects. It offers a broader range of random data generation compared to @thi.ng/random, which is more focused on numerical randomization.
@thi.ng/random
This project is part of the
@thi.ng/umbrella monorepo.
About
This package provides the IRandom
interface and various (mostly
seedable) pseudo-random number generator implementations, incl. a
IRandom
wrapper for Math.random()
:
Installation
yarn add @thi.ng/random
Dependencies
Usage examples
import { Smush32 } from "@thi.ng/random";
const rnd = new Smush32(0xdecafbad);
rnd.int()
rnd.float()
rnd.norm(100)
rnd.minmax(10, 20)
rnd.gaussian()
Authors
License
© 2018 Karsten Schmidt // Apache Software License 2.0