Class Rand

java.lang.Object
games.stendhal.common.Rand

public class Rand extends Object
Helper functions to generate random numbers.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static boolean
    flipCoin​(double propability)
    Flip a coin to decide between true and false based on a probability
    static double
    propabilityForMeanExp​(long mean)
    Calculate the probability for a given mean value in an exponential distribution
    static double
    Generates an equally distributed double precision random number.
    static int
    rand​(int n)
    Generates an equally distributed random number.
    static <T> T
    rand​(List<T> list)
    Given a list of any type, returns an arbitrary element, using an equal distribution.
    static <T> T
    rand​(Set<T> set)
    Given a set of any type, returns an arbitrary element, using an equal distribution.
    static <T> T
    rand​(T[] array)
    Given a array of any type, returns an arbitrary element, using an equal distribution.
    static int
    randExponential​(int mean)
    Generates an exponentially distributed random number and rounds it.
    static int
    randGaussian​(int mean, int sd)
    Generates a normally distributed random number and rounds it.
    static int
    randUniform​(int a, int b)
    Generates an equally distributed random number between a and b inclusive It doesn't matter if a or b is bigger.
    static int
    Simulates rolling a dice with 100 sides.
    static int
    Simulates rolling a dice with 20 sides.
    static int
    Simulates rolling a dice with 6 sides.
    static int
    Simulates flipping a coin.

    Methods inherited from class java.lang.Object

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

    • Rand

      public Rand()
  • Method Details

    • throwCoin

      public static int throwCoin()
      Simulates flipping a coin.
      Returns:
      Either 1 or 2, equally distributed.
    • roll1D6

      public static int roll1D6()
      Simulates rolling a dice with 6 sides.
      Returns:
      A random number between 1 and 6, equally distributed.
    • roll1D20

      public static int roll1D20()
      Simulates rolling a dice with 20 sides.
      Returns:
      A random number between 1 and 20, equally distributed.
    • roll1D100

      public static int roll1D100()
      Simulates rolling a dice with 100 sides.
      Returns:
      A random number between 1 and 100, equally distributed.
    • randUniform

      public static int randUniform(int a, int b)
      Generates an equally distributed random number between a and b inclusive It doesn't matter if a or b is bigger.
      Parameters:
      a - the first boundary number (upper or lower)
      b - the second boundary number (upper or lower)
      Returns:
      A random number between a and b, equally distributed.
    • rand

      public static int rand(int n)
      Generates an equally distributed random number.
      Parameters:
      n - the upper boundary
      Returns:
      A random number between 0 and n - 1, equally distributed.
    • rand

      public static double rand()
      Generates an equally distributed double precision random number. the upper boundary
      Returns:
      A random number between 0 and 1, equally distributed.
    • rand

      public static <T> T rand(List<T> list)
      Given a list of any type, returns an arbitrary element, using an equal distribution. Generics are used so that the returned element will have the same type as the list's elements have.
      Type Parameters:
      T - Any type.
      Parameters:
      list - The list from which an element should be chosen.
      Returns:
      A random list element.
    • rand

      public static <T> T rand(Set<T> set)
      Given a set of any type, returns an arbitrary element, using an equal distribution. Generics are used so that the returned element will have the same type as the set's elements have. NOTE: This is not very efficient. If you need to do this on large sets several times per second, consider copying the set contents to an array, then call rand() on this array.
      Type Parameters:
      T - Any type.
      Parameters:
      set - The set from which an element should be chosen.
      Returns:
      A random set element.
    • rand

      public static <T> T rand(T[] array)
      Given a array of any type, returns an arbitrary element, using an equal distribution. Generics are used so that the returned element will have the same type as the array's elements have.
      Type Parameters:
      T - Any type.
      Parameters:
      array - The array from which an element should be chosen.
      Returns:
      A random array element.
    • randGaussian

      public static int randGaussian(int mean, int sd)
      Generates a normally distributed random number and rounds it.
      Parameters:
      mean - The mean value
      sd - The standard deviation
      Returns:
      An integer near mean
    • randExponential

      public static int randExponential(int mean)
      Generates an exponentially distributed random number and rounds it.
      Parameters:
      mean - The mean value
      Returns:
      An integer exponential variate mean
    • propabilityForMeanExp

      public static double propabilityForMeanExp(long mean)
      Calculate the probability for a given mean value in an exponential distribution
      Parameters:
      mean - the desired mean value of the distribution
      Returns:
      the probability to reach the given mean value (1 for mean == 0)
    • flipCoin

      public static boolean flipCoin(double propability)
      Flip a coin to decide between true and false based on a probability
      Parameters:
      propability - the probability to get true
      Returns:
      true or false randomly