Skip to content
Eric Yancey Dauenhauer edited this page Feb 2, 2024 · 2 revisions

SalamiVG ships with a pseudo-random number generator, and helper functions to make your life easier.

Types

  • Rng: This is a type used to refer to a function which returns pseudo-random numbers in the range [0, 1). The built-in Math.random() function meets this criteria, and is the default in any function that requires an Rng. However, if you want finer control over your output, you may want to use a seedable Rng.
  • Integer: a number which should be an integer. Of course, JavaScript does not have an Integer data type. This is simply a type alias used to indicate where integers should be used.
  • Decimal: a number which should be a floating-point numeric type.
  • number: any JavaScript number.

Functions

  • createRng(seed?: string = Date.now().toString()): Rng: Returns an Rng function seeded with the given seed value.
  • random(min: number, max: number, rng = Math.random): number: Returns a random number in range [min, max).
  • randomInt(min: Integer, max: Integer, rng = Math.random): Integer: Returns a random integer in the range [min, max).
  • shuffle<T>(array: T[], rng = Math.random): T[]: Shuffles an array. Returns a new array instance; does not shuffle in place.
  • randomFromArray<T>(array: T[], rng = Math.random): T: Returns a single random value from an array.
  • randomFromObject<T>(object: Record<string, T>, rng = Math.random): T: Returns a single random value from an object. Note: if you need a random key, you'll need to use randomFromArray(Object.keys(object), rng)
  • randomSeed(): string: A helper function to return a random seed value that can be used in createRng()