Class Calculations

java.lang.Object
com.valhalla.sdk.api.methods.Calculations

public final class Calculations extends Object
Deterministic-testable timing, distance, and random helpers for scripts.
  • Method Summary

    Modifier and Type
    Method
    Description
    static boolean
    chance(int target)
    Performs a percentage chance roll.
    static boolean
    chance(int target, int overall)
    Performs a target / overall chance roll.
    static double
    distance(int x1, int y1, int x2, int y2)
    Computes Euclidean distance between two coordinates.
    static double
    distance(Tile first, Tile second)
    Computes Euclidean distance between two tiles, ignoring plane.
    static long
    elapsed(long previousTime)
    Computes elapsed wall-clock time.
    static String
    Returns a 16-character alphanumeric pseudo-random string.
    static Random
    Returns the shared pseudo-random generator used by this utility.
    static boolean
    isBefore(long futureTime)
    Tests a wall-clock deadline.
    static double
    nextGammaRandom(int shape, int scale)
    Compatibility alias for the distribution helper exposed by Randoms.
    static double
    nextGaussianRandom(double mean, double sigma)
    Samples a Gaussian distribution.
    static double
    nextHypergeometricRandom(int populationSize, int numberOfSuccesses, int sampleSize)
    Compatibility alias for the distribution helper exposed by Randoms.
    static double
    nextLogNormalDistributionRandom(double scale, double shape)
    Compatibility alias for the distribution helper exposed by Randoms.
    static double
    random(double minimum, double maximum)
    Samples a finite double strictly between two bounds.
    static int
    random(int high)
    Samples an integer from [0, high).
    static int
    random(int minimum, int maximum)
    Samples an integer from [minimum, maximum).
    static long
    random(long minimum, long maximum)
    Samples a long from [minimum, maximum) without modulo bias.
    static void
    setRandomSeed(long seed)
    Sets the deterministic seed for subsequent pseudo-random calls.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • getRandom

      public static Random getRandom()
      Returns the shared pseudo-random generator used by this utility.
      Returns:
      the shared pseudo-random generator used by this utility
    • setRandomSeed

      public static void setRandomSeed(long seed)
      Sets the deterministic seed for subsequent pseudo-random calls. @param seed new seed
    • random

      public static int random(int high)
      Samples an integer from [0, high).
      Parameters:
      high - exclusive upper bound; must be positive
      Returns:
      a sampled integer
    • random

      public static int random(int minimum, int maximum)
      Samples an integer from [minimum, maximum).
      Parameters:
      minimum - inclusive lower bound
      maximum - exclusive upper bound
      Returns:
      a sampled integer
    • random

      public static long random(long minimum, long maximum)
      Samples a long from [minimum, maximum) without modulo bias.
      Parameters:
      minimum - inclusive lower bound
      maximum - exclusive upper bound
      Returns:
      a sampled long
    • random

      public static double random(double minimum, double maximum)
      Samples a finite double strictly between two bounds.
      Parameters:
      minimum - exclusive lower bound
      maximum - exclusive upper bound
      Returns:
      a sampled double
    • chance

      public static boolean chance(int target)
      Performs a percentage chance roll.
      Parameters:
      target - successful percentage points out of 100
      Returns:
      the result of the roll
    • chance

      public static boolean chance(int target, int overall)
      Performs a target / overall chance roll.
      Parameters:
      target - successful outcomes
      overall - total outcomes; must be positive
      Returns:
      the result of the roll
    • distance

      public static double distance(int x1, int y1, int x2, int y2)
      Computes Euclidean distance between two coordinates.
      Parameters:
      x1 - first x coordinate
      y1 - first y coordinate
      x2 - second x coordinate
      y2 - second y coordinate
      Returns:
      Euclidean distance
    • distance

      public static double distance(Tile first, Tile second)
      Computes Euclidean distance between two tiles, ignoring plane.
      Parameters:
      first - first tile
      second - second tile
      Returns:
      Euclidean distance
    • elapsed

      public static long elapsed(long previousTime)
      Computes elapsed wall-clock time.
      Parameters:
      previousTime - earlier epoch time in milliseconds
      Returns:
      non-negative elapsed milliseconds
    • isBefore

      public static boolean isBefore(long futureTime)
      Tests a wall-clock deadline.
      Parameters:
      futureTime - deadline as epoch milliseconds
      Returns:
      true before the deadline
    • nextGaussianRandom

      public static double nextGaussianRandom(double mean, double sigma)
      Samples a Gaussian distribution.
      Parameters:
      mean - distribution mean
      sigma - non-negative standard deviation
      Returns:
      a sampled value
    • nextLogNormalDistributionRandom

      public static double nextLogNormalDistributionRandom(double scale, double shape)
      Compatibility alias for the distribution helper exposed by Randoms.
      Parameters:
      scale - positive distribution scale
      shape - positive distribution shape
      Returns:
      a sampled log-normal value
    • nextGammaRandom

      public static double nextGammaRandom(int shape, int scale)
      Compatibility alias for the distribution helper exposed by Randoms.
      Parameters:
      shape - positive integer shape
      scale - positive integer scale
      Returns:
      a sampled gamma value
    • nextHypergeometricRandom

      public static double nextHypergeometricRandom(int populationSize, int numberOfSuccesses, int sampleSize)
      Compatibility alias for the distribution helper exposed by Randoms.
      Parameters:
      populationSize - positive population size
      numberOfSuccesses - successful members in the population
      sampleSize - number of members drawn without replacement
      Returns:
      the sampled number of successes
    • generateRandomString

      public static String generateRandomString()
      Returns a 16-character alphanumeric pseudo-random string.
      Returns:
      a 16-character alphanumeric pseudo-random string