random

  • Version 5.5.1
  • Published
  • 104 kB
  • No dependencies
  • MIT license

Install

npm i random
yarn add random
pnpm add random

Overview

Seedable random number generator supporting many common distributions.

Index

Variables

variable _default

const _default: Random;

    Functions

    function createRNG

    createRNG: (seedOrRNG?: SeedOrRNG) => RNG;

      function mixKey

      mixKey: (seed: Seed, key: number[]) => number[];
      • Mixes a string seed into a key that is an array of integers, and returns a shortened string seed that is equivalent to the result key.

      function shuffleInPlace

      shuffleInPlace: <T>(gen: RNG, array: Array<T>) => void;

        function sparseFisherYates

        sparseFisherYates: <T>(gen: RNG, array: Array<T>, k: number) => T[];
        • Fisher-Yates sampling without replacement O(k) time and space, by using a hash table instead of a full copy of the array see https://arxiv.org/pdf/2104.05091 Algorithm 2

        Classes

        class ARC4RNG

        class ARC4RNG extends RNG {}

          constructor

          constructor(seed?: Seed);

            property i

            i: number;

              property j

              j: number;

                property name

                readonly name: string;

                  property S

                  S: number[];

                    method clone

                    clone: () => ARC4RNG;

                      method g

                      g: (count: number) => number;

                        method next

                        next: () => number;

                          class FunctionRNG

                          class FunctionRNG extends RNG {}

                            constructor

                            constructor(rngFn: RNGFn);

                              property name

                              readonly name: string;

                                method clone

                                clone: () => FunctionRNG;

                                  method next

                                  next: () => number;

                                    class MathRandomRNG

                                    class MathRandomRNG extends RNG {}

                                      property name

                                      readonly name: string;

                                        method clone

                                        clone: () => MathRandomRNG;

                                          method next

                                          next: () => number;

                                            class Random

                                            class Random {}
                                            • Seedable random number generator supporting many common distributions.

                                              Random

                                              Parameter rng

                                              Underlying random number generator or a seed for the default PRNG. Defaults to Math.random.

                                            constructor

                                            constructor(seedOrRNG?: SeedOrRNG);

                                              property rng

                                              readonly rng: RNG;
                                              • {RNG} rng - Underlying pseudo-random number generator.

                                              method bates

                                              bates: (n?: number) => IDist<number>;
                                              • Generates a [Bates distribution](https://en.wikipedia.org/wiki/Bates_distribution).

                                                Parameter n

                                                Number of uniform samples to average (n >= 1)

                                              method bernoulli

                                              bernoulli: (p?: number) => IDist<number>;
                                              • Generates a [Bernoulli distribution](https://en.wikipedia.org/wiki/Bernoulli_distribution).

                                                Parameter p

                                                Success probability of each trial.

                                              method binomial

                                              binomial: (n?: number, p?: number) => IDist<number>;
                                              • Generates a [Binomial distribution](https://en.wikipedia.org/wiki/Binomial_distribution).

                                                Parameter n

                                                Number of trials.

                                                Parameter p

                                                Success probability of each trial.

                                              method bool

                                              bool: () => boolean;
                                              • Samples a uniform random boolean value.

                                                Convenience wrapper around random.uniformBoolean()

                                                random.boolean

                                              method boolean

                                              boolean: () => boolean;
                                              • Samples a uniform random boolean value.

                                                Convenience wrapper around random.uniformBoolean()

                                              method choice

                                              choice: <T>(array: Array<T>, weights?: Array<number>) => T | undefined;
                                              • Returns an item chosen uniformly at random from the given array. If weights are provided, returns an item based on weighted probabilities.

                                                Convenience wrapper around random.uniformInt() for uniform selection, or implements weighted selection using cumulative distribution.

                                                Parameter array

                                                Input array

                                                Parameter weights

                                                Optional weights for each item (must be same length as array)

                                              method clone

                                              clone: (seedOrRNG?: SeedOrRNG) => Random;
                                              • Creates a new Random instance, optionally specifying parameters to set a new seed.

                                              method exponential

                                              exponential: (lambda?: number) => IDist<number>;
                                              • Generates an [Exponential distribution](https://en.wikipedia.org/wiki/Exponential_distribution).

                                                Parameter lambda

                                                Inverse mean (lambda > 0)

                                              method float

                                              float: (min?: number, max?: number) => number;
                                              • Samples a uniform random floating point number, optionally specifying lower and upper bounds.

                                                Convenience wrapper around random.uniform()

                                                Parameter min

                                                Lower bound (float, inclusive)

                                                Parameter max

                                                Upper bound (float, exclusive)

                                              method geometric

                                              geometric: (p?: number) => IDist<number>;
                                              • Generates a [Geometric distribution](https://en.wikipedia.org/wiki/Geometric_distribution).

                                                Parameter p

                                                Success probability of each trial.

                                              method int

                                              int: (min?: number, max?: number) => number;
                                              • Samples a uniform random integer, optionally specifying lower and upper bounds.

                                                Convenience wrapper around random.uniformInt()

                                                Parameter min

                                                Lower bound (integer, inclusive)

                                                Parameter max

                                                Upper bound (integer, inclusive)

                                              method integer

                                              integer: (min?: number, max?: number) => number;
                                              • Samples a uniform random integer, optionally specifying lower and upper bounds.

                                                Convenience wrapper around random.uniformInt()

                                                random.int

                                                Parameter min

                                                Lower bound (integer, inclusive)

                                                Parameter max

                                                Upper bound (integer, inclusive)

                                              method irwinHall

                                              irwinHall: (n?: number) => IDist<number>;
                                              • Generates an [Irwin Hall distribution](https://en.wikipedia.org/wiki/Irwin%E2%80%93Hall_distribution).

                                                Parameter n

                                                Number of uniform samples to sum (n >= 0)

                                              method logNormal

                                              logNormal: (mu?: number, sigma?: number) => IDist<number>;
                                              • Generates a [Log-normal distribution](https://en.wikipedia.org/wiki/Log-normal_distribution).

                                                Parameter mu

                                                Mean of underlying normal distribution

                                                Parameter sigma

                                                Standard deviation of underlying normal distribution

                                              method next

                                              next: () => number;
                                              • Convenience wrapper around this.rng.next()

                                                Returns a floating point number in [0, 1).

                                                {number}

                                              method normal

                                              normal: (mu?: number, sigma?: number) => IDist<number>;
                                              • Generates a [Normal distribution](https://en.wikipedia.org/wiki/Normal_distribution).

                                                Parameter mu

                                                Mean

                                                Parameter sigma

                                                Standard deviation

                                              method pareto

                                              pareto: (alpha?: number) => IDist<number>;
                                              • Generates a [Pareto distribution](https://en.wikipedia.org/wiki/Pareto_distribution).

                                                Parameter alpha

                                                Alpha

                                              method poisson

                                              poisson: (lambda?: number) => IDist<number>;
                                              • Generates a [Poisson distribution](https://en.wikipedia.org/wiki/Poisson_distribution).

                                                Parameter lambda

                                                Mean (lambda > 0)

                                              method sample

                                              sample: <T>(array: Array<T>, k: number) => Array<T>;
                                              • Returns a random subset of k items from the given array (without replacement).

                                                Parameter array

                                                Input array

                                              method sampler

                                              sampler: <T>(array: Array<T>, k: number) => () => Array<T>;
                                              • Generates a thunk which returns samples of size k from the given array.

                                                This is for convenience only; there is no gain in efficiency.

                                                Parameter array

                                                Input array

                                              method shuffle

                                              shuffle: <T>(array: Array<T>) => Array<T>;
                                              • Returns a shuffled copy of the given array.

                                                Parameter array

                                                Input array

                                              method shuffler

                                              shuffler: <T>(array: Array<T>) => () => Array<T>;
                                              • Generates a thunk which returns shuffled copies of the given array.

                                                Parameter array

                                                Input array

                                              method uniform

                                              uniform: (min?: number, max?: number) => IDist<number>;
                                              • Generates a [Continuous uniform distribution](https://en.wikipedia.org/wiki/Uniform_distribution_(continuous)).

                                                Parameter min

                                                Lower bound (float, inclusive)

                                                Parameter max

                                                Upper bound (float, exclusive)

                                              method uniformBoolean

                                              uniformBoolean: () => IDist<boolean>;
                                              • Generates a [Discrete uniform distribution](https://en.wikipedia.org/wiki/Discrete_uniform_distribution), with two possible outcomes, true or `false.

                                                This method is analogous to flipping a coin.

                                              method uniformInt

                                              uniformInt: (min?: number, max?: number) => IDist<number>;
                                              • Generates a [Discrete uniform distribution](https://en.wikipedia.org/wiki/Discrete_uniform_distribution).

                                                Parameter min

                                                Lower bound (integer, inclusive)

                                                Parameter max

                                                Upper bound (integer, inclusive)

                                              method use

                                              use: (seedOrRNG: SeedOrRNG) => void;
                                              • Sets the underlying pseudorandom number generator.

                                                Example 1

                                                import random from 'random'
                                                random.use('example-seed')
                                                // or
                                                random.use(Math.random)

                                              method weibull

                                              weibull: (lambda: number, k: number) => () => number;
                                              • Generates a [Weibull distribution](https://en.wikipedia.org/wiki/Weibull_distribution).

                                                Parameter lambda

                                                Lambda, the scale parameter

                                                Parameter k

                                                k, the shape parameter

                                              class RNG

                                              abstract class RNG {}

                                                property name

                                                readonly name: string;

                                                  method clone

                                                  abstract clone: () => RNG;

                                                    method next

                                                    abstract next: () => number;

                                                      class XOR128RNG

                                                      class XOR128RNG extends RNG {}

                                                        constructor

                                                        constructor(seed?: Seed);

                                                          property name

                                                          readonly name: string;

                                                            property w

                                                            w: number;

                                                              property x

                                                              x: number;

                                                                property y

                                                                y: number;

                                                                  property z

                                                                  z: number;

                                                                    method clone

                                                                    clone: () => XOR128RNG;

                                                                      method next

                                                                      next: () => number;

                                                                        class Xoshiro128StarStarRNG

                                                                        class Xoshiro128StarStarRNG extends RNG {}
                                                                        • xoshiro128** is a small, fast, general-purpose pseudorandom number generator with 128 bits of state and a period of 2^128 - 1.

                                                                          It is not cryptographically secure.

                                                                          See Also

                                                                          • https://prng.di.unimi.it/xoshiro128starstar.c

                                                                        constructor

                                                                        constructor(seed?: Seed);

                                                                          property name

                                                                          readonly name: string;

                                                                            property s0

                                                                            protected s0: number;

                                                                              property s1

                                                                              protected s1: number;

                                                                                property s2

                                                                                protected s2: number;

                                                                                  property s3

                                                                                  protected s3: number;

                                                                                    method clone

                                                                                    clone: () => Xoshiro128StarStarRNG;

                                                                                      method next

                                                                                      next: () => number;

                                                                                        method nextUint32

                                                                                        protected nextUint32: () => number;

                                                                                          method setState

                                                                                          protected setState: (state: [number, number, number, number]) => void;

                                                                                            Type Aliases

                                                                                            type RNGFn

                                                                                            type RNGFn = () => number;

                                                                                              type Seed

                                                                                              type Seed = number | string;

                                                                                                type SeedOrRNG

                                                                                                type SeedOrRNG = number | string | RNGFn | RNG;

                                                                                                  Package Files (1)

                                                                                                  Dependencies (0)

                                                                                                  No dependencies.

                                                                                                  Dev Dependencies (15)

                                                                                                  Peer Dependencies (0)

                                                                                                  No peer dependencies.

                                                                                                  Badge

                                                                                                  To add a badge like this onejsDocs.io badgeto your package's README, use the codes available below.

                                                                                                  You may also use Shields.io to create a custom badge linking to https://www.jsdocs.io/package/random.

                                                                                                  • Markdown
                                                                                                    [![jsDocs.io](https://img.shields.io/badge/jsDocs.io-reference-blue)](https://www.jsdocs.io/package/random)
                                                                                                  • HTML
                                                                                                    <a href="https://www.jsdocs.io/package/random"><img src="https://img.shields.io/badge/jsDocs.io-reference-blue" alt="jsDocs.io"></a>