Skip to main content

Random

Pseudo-random number generator following the Mersenne_Twister algorithm. Given a seed this generator will produce the same sequence of numbers each time it is called. See https://en.wikipedia.org/wiki/Mersenne_Twister for more details. Uses the MT19937-32 (2002) implementation documented here http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/MT2002/emt19937ar.html

Api inspired by http://chancejs.com/# https://github.com/chancejs/chancejs

Index

Constructors

constructor

  • new Random(seed?: number): Random
  • If no seed is specified, the Date.now() is used


    Parameters

    • optionalseed: number

    Returns Random

Properties

publicoptionalseed

seed?: number

Methods

publicbool

  • bool(likelihood?: number): boolean
  • Returns true or false randomly with 50/50 odds by default. By default the likelihood of returning a true is .5 (50%).


    Parameters

    • likelihood: number = 0.5

      takes values between [0, 1]

    Returns boolean

publicd10

  • d10(): number
  • Returns the result of a d10 dice roll


    Returns number

publicd12

  • d12(): number
  • Returns the result of a d12 dice roll


    Returns number

publicd20

  • d20(): number
  • Returns the result of a d20 dice roll


    Returns number

publicd4

  • d4(): number
  • Returns the result of a d4 dice roll


    Returns number

publicd6

  • d6(): number
  • Returns the result of a d6 dice roll


    Returns number

publicd8

  • d8(): number
  • Returns the result of a d8 dice roll


    Returns number

publicfloating

  • floating(min: number, max: number): number
  • Return a random floating point in range [min, max) min is included, max is not included


    Parameters

    • min: number
    • max: number

    Returns number

publicinteger

  • integer(min: number, max: number): number

publicnext

  • next(): number
  • Return a random floating point number between [0, 1)


    Returns number

publicnextInt

  • nextInt(): number
  • Return next 32 bit integer number in sequence


    Returns number

publicpickOne

  • pickOne<T>(array: T[]): T
  • Returns one element from an array at random


    Type parameters

    • T

    Parameters

    • array: T[]

    Returns T

publicpickSet

  • pickSet<T>(array: T[], numPicks: number, allowDuplicates?: boolean): T[]
  • Returns a new array random picking elements from the original


    Type parameters

    • T

    Parameters

    • array: T[]

      Original array to pick from

    • numPicks: number

      can be any positive number

    • allowDuplicates: boolean = false

      indicates whether the returned set is allowed duplicates (it does not mean there will always be duplicates just that it is possible)

    Returns T[]

publicrange

  • range(length: number, min: number, max: number): number[]
  • Generate a list of random integer numbers


    Parameters

    • length: number

      the length of the final array

    • min: number

      the minimum integer number to generate inclusive

    • max: number

      the maximum integer number to generate inclusive

    Returns number[]

publicshuffle

  • shuffle<T>(array: T[]): T[]