Class Die<T>

A Die is a wrapper over an immutable Map, where keys correspond to possible outcomes of rolling the die, and values determine the probability of the respective outcome.

Conceptually, any combination of dice is aggregated into a new Die which contains all possible outcomes of the combination.

Type Parameters

Hierarchy

  • Die

Constructors

Properties

outcomes: Map<T, number>

Methods

  • Re-interprets the outcomes of a Die using an arbitrary mapping function.

    All outcomes which mapped to the same value are merged into a single outcome with combined probability.

    Type Parameters

    Parameters

    • f: ((outcome) => U)

      mapping function

        • (outcome): U
        • Parameters

          • outcome: T

          Returns U

    Returns Die<U>

    new die with re-interpreted outcomes

  • Ensures that probabilities of all outcomes of the Die add up to 1 (divides all probabilities by their sum). This is done in place without creating a new die.

    Returns Die<T>

  • Re-interprets the outcomes of a Die using an arbitrary mapping function. Unlike Die.interpret, this function can map each outcome to a distribution of multiple possible new outcomes, represented by a Die. The probabilities of all outcomes in this new Die should add up to 1, the Die.reroll function takes care of the conditional probabilities.

    All outcomes which mapped to the same value are merged into a single outcome with combined probability. Comparison between outcomes is performed using fast-deep-equal.

    Type Parameters

    Parameters

    • f: ((outcome) => Die<U>)

      mapping function

        • (outcome): Die<U>
        • Parameters

          • outcome: T

          Returns Die<U>

    Returns Die<U>

    new die with re-interpreted outcomes

  • Produce a single die: d6, d8, d20, etc. The number numSides can be any positive integer. Each side has an equal probability.

    Parameters

    • numSides: number

      number of sides

    Returns Die<number>

  • Produce an array of identical dice: 2d6, 5d8, etc. The numbers numDice and numSides can be any positive integers. Each side has an equal probability.

    Parameters

    • numDice: number

      number of dice

    • numSides: number

      number of sides each die has

    Returns Indexed<Die<number>>

  • Combines two dice using an arbitrary rule for interpreting the result.

    Type Parameters

    Parameters

    • combine: ((first, second) => V)

      function which determines how the Die values should be combined

        • (first, second): V
        • Parameters

          • first: T
          • second: U

          Returns V

    • dieA: Die<T>

      first die

    • dieB: Die<U>

      second die

    Returns Die<V>

    new combined die

    See

    opposed.ts example

  • Internal function used in Die.pair().

    Type Parameters

    Parameters

    • combine: ((first, second) => V)

      function which determines how the outcomes should be combined

        • (first, second): V
        • Parameters

          • first: T
          • second: U

          Returns V

    • dieA: Map<T, number>

      outcomes of the first die

    • dieB: Map<U, number>

      outcomes of the second die

    Returns Map<V, number>

    new combined outcomes

  • Combines a pool of dice into a single Die using an arbitrary aggregation rule.

    Type Parameters

    Parameters

    • accumulatorCallback: AccumulatorCallback<T, U>
    • initial: U

      initial value of the accumulator variable

    • dice: Iterable<Die<T>>

      array of dice to combine into a pool

    Returns Die<U>

    pool all possible outcome combinations aggregated as a single Die

  • Returns a new Die which always produces the same outcome.

    Type Parameters

    Parameters

    • outcome: T

    Returns Die<T>

Generated using TypeDoc