Options
All
  • Public
  • Public/Protected
  • All
Menu

External module "modules/utils"

Index

Functions

pickRandom

  • pickRandom<T>(arr: ReadonlyArray<T>, subarrayRange?: IntegerRange): T
  • Returns a random item from the given array. If array.length is 0 or subarrayRange goes beyond array index limits, may return undefined.

    Type parameters

    • T

    Parameters

    • arr: ReadonlyArray<T>

      Array to get random item from.

    • Default value subarrayRange: IntegerRange = new IntegerRange(0, arr.length)

      IntegerRange which defines subarray indexes to pick items from, if not specified, takes items from the whole array.

    Returns T

pickRandomItems

  • pickRandomItems<T>(arr: T[], times: number[]): IterableIterator<T>
  • Returns IterableIterator which randomly picks each arr[i] value at most times[i] times, thus creates the _.sum(times) iterations.

    copyright

    https://stackoverflow.com/a/196065/9259330

    remarks

    Prerequisites: arr.length === times.length and each times[i] >= 1. Beware that this function takes ownership of arr and times references. times and arr array values get randomly shuffled, moreover times[i] gets decreased each time arr[i] value was picked. Thus you should copy arr and times array if those changes are unacceptable, e.g. pickRandomItems([...arr], [...times])

    Type parameters

    • T

    Parameters

    • arr: T[]

      Array to pick values from

    • times: number[]

      Array of numbers of times a value can be picked from arr

    Returns IterableIterator<T>

swapItems

  • swapItems<T>(arr: T[], i: number, j: number): void
  • Swaps items in arr at indexes i, j.

    Type parameters

    • T

    Parameters

    • arr: T[]

      Array to swap items in.

    • i: number

      Index of arr item to swap with arr[j]

    • j: number

      Index of arr item to swap with arr[i]

    Returns void

Generated using TypeDoc