Class Random

This provides a random number generator that can be seeded. Math.rand() cannot be seeded. Using a seed will allow me to repeat things in the debugger when my program acts strange.

Methods

  • Parameters

    • input: string

      Any string is valid. Reasonable inputs include "My game", "My game 32", "My game 33", "在你用中文测试过之前你还没有测试过它。". I.e. you might just add or change one character, and you want to maximize the resulting change.

    Returns string

  • Create a new instance of a random number generator.

    Also consider Random.fromString() which is slightly newer. This only works with seeds that have been created and saved by this class. Random.fromString() can turn any string into a seed.

    Parameters

    • seed: string = ...

      The result from a previous call to Random.newSeed(). By default this will create a new seed. Either way the seed will be sent to the JavaScript console.

      Typical use: Use the default until you want to repeat something. Then copy the last seed from the log and use here.

    Returns RandomFunction

    A function that can be used as a drop in replacement for Math.random().

    If the seed is invalid this will throw an Error.

  • Create a new random number generator based on a string. The result will be repeatable. I.e. the same input will always lead the the same random number generator.

    Parameters

    • s: string

      Any string is acceptable. This can include random things like "try again 27".

      And it can include special things like "[1,2,3,4]" which are generated by this library. randomNumberGenerator.currentSeed() will return a seed that can be used to clone the random number generator in its current state.

    Returns RandomFunction

    A new random number generator.

  • Returns string

    A new seed value appropriate for use in a call to Random.create(). This will be reasonably random.

    The seed is intended to be opaque, a magic cookie. It's something that's easy to copy and paste. Don't try to parse or create one of these.

  • Returns true if this was a valid seed created by RandomFunction.currentSeed or Random.newSeed().

    Parameters

    • seed: string

      The string to test

    Returns boolean

    True if this was a saved seed value.