mikera.util
Class Rand
- java.lang.Object
-
- mikera.util.Rand
-
public final class Rand extends java.lang.Object
-
-
Constructor Summary
Constructors Constructor and Description Rand()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method and Description static intbest(int r, int n, int s)sum of best r from n s-sided dicestatic doublebinary(double mean)static voidbinarySample(double[] temp, int offset, int length)Randomises the temp array to 0.0 or 1.0 according to the probabilities in the arraystatic voidbinarySample(float[] temp, int offset, int length)static doublebinomialChance(int r, int n, double p)static booleanchance(double d)Returns true with a given probabilitystatic booleanchance(float d)Returns true with a given probabilitystatic voidchooseIntegers(int[] dest, int destOffset, int length, int maxValue)Chooses a set of distinct integers from a range 0 to maxValue-1 Resulting integers are sortedstatic doublecombinations(int r, int n)static doublecumulativeBinomialChance(int r, int n, double p)Returns the probability of up to r successes in n trials, each with probability of success pstatic intd(int sides)simulates a dice roll with the given number of sidesstatic intd(int number, int sides)Calculates the sum of (number) x (sides)-sided dicestatic intd10()static intd100()static intd12()static intd20()static intd3()static intd4()static intd6()static intd8()static doubleexp(double mean)Returns a random sample from an exponential distributionstatic doublefactorial(int n)static doublefactorialRatio(int n, int r)Calculates n!/r! Avoids the overhead of calculating the terms which cancel outstatic voidfillBinary(double[] data, int start, int length, double mean)static voidfillBinary(float[] d, int start, int length)static voidfillGaussian(double[] d, int start, int length, double u, double sd)static voidfillGaussian(float[] d, int start, int length, float u, float sd)static voidfillUniform(float[] d, int start, int length)static intgeom(double p)Returns a sample from a Geometric distribution Discrete distribution with decay rate p Mean = (1-p) / p Probability mass function for each integer output k = p.(1-p)^kstatic intindexFromWeights(double[] probabilities)static doublen(double u, double sd)Generates a normal distributed number with the given mean and standard deviationstatic booleannextBoolean()Sample a random boolean value with 50% chancestatic bytenextByte()Sample a random (signed) bytestatic charnextChar()Sample a random charstatic doublenextDouble()Returns standard double in range [0..1)static floatnextFloat()Sample a random float in range [0..1)static doublenextGaussian()static intnextInt()Sample a random signed 32-bit integerstatic charnextLetter()Sample a random lowercase letterstatic java.lang.StringnextLetterString(int length)static longnextLong()Gets a long random valuestatic shortnextShort()Sample a random signed short valuestatic java.lang.StringnextString()static intotherIndex(int i, int max)Return a random index between 0 and max (exclusive) not equal to istatic <T> Tpick(java.util.Collection<T> ts)Picks a random item from a given collectionstatic <T> Tpick(java.util.List<T> ts)Picks a random item from a given liststatic <T> Tpick(T[] ts)Picks a random item from a given arraystatic intpo(double mean)Poisson distributionstatic intpo(int numerator, int denominator)Draw a sample from the possion distribution with parameter = numerator / denominatorstatic intr(int s)Random number from zero to s-1static voidrandIntegers(int[] dest, int destOffset, int length, int maxValue)Creates a set of n random integers from a range of 0 to maxValue-1static intrange(int n1, int n2)Returns random number uniformly distributed in inclusive [n1, n2] range.static intround(double d)Randomly rounds to the nearest integerstatic <T> voidshuffle(T[] ts)Randomly shuffles all elements in a given arraystatic intsig(float x)logistic sigmoid probabilitystatic doublesigmoid(double a)Sigmoid functionstatic doubleu()Sample a random uniform double in range [0..1)static doubleu(double max)Sample a random uniform double in range [0..max)static doubleu(double min, double max)Sample a random uniform double in the range [min..max)static intxorShift32(int a)XORShift algorithm - credit to George Marsaglia!static longxorShift64(long a)XORShift algorithm - very fast psuedo-random number generator Credit to George Marsaglia!
-
-
-
Method Detail
-
nextLong
public static final long nextLong()
Gets a long random value- Returns:
- Random long value based on static state
-
xorShift64
public static final long xorShift64(long a)
XORShift algorithm - very fast psuedo-random number generator Credit to George Marsaglia!- Parameters:
a- Initial state- Returns:
- new state
-
xorShift32
public static final int xorShift32(int a)
XORShift algorithm - credit to George Marsaglia!- Parameters:
a- Initial state- Returns:
- new state
-
chance
public static boolean chance(double d)
Returns true with a given probability- Parameters:
d-- Returns:
-
chance
public static boolean chance(float d)
Returns true with a given probability- Parameters:
d-- Returns:
-
best
public static int best(int r, int n, int s)sum of best r from n s-sided dice- Parameters:
r-n-s-- Returns:
-
sigmoid
public static double sigmoid(double a)
Sigmoid function
-
sig
public static int sig(float x)
logistic sigmoid probability- Parameters:
x-- Returns:
-
po
public static int po(double mean)
Poisson distribution- Parameters:
mean- The mean of the poisson distrubution to sample from- Returns:
-
po
public static int po(int numerator, int denominator)Draw a sample from the possion distribution with parameter = numerator / denominator- Parameters:
numerator-denominator-- Returns:
-
d
public static int d(int number, int sides)Calculates the sum of (number) x (sides)-sided dice- Parameters:
number-sides-- Returns:
-
factorial
public static double factorial(int n)
-
factorialRatio
public static double factorialRatio(int n, int r)Calculates n!/r! Avoids the overhead of calculating the terms which cancel out- Parameters:
n-r-- Returns:
-
cumulativeBinomialChance
public static double cumulativeBinomialChance(int r, int n, double p)Returns the probability of up to r successes in n trials, each with probability of success p- Parameters:
r-n-p-- Returns:
-
combinations
public static double combinations(int r, int n)
-
binomialChance
public static double binomialChance(int r, int n, double p)
-
exp
public static double exp(double mean)
Returns a random sample from an exponential distribution- Parameters:
mean-- Returns:
-
geom
public static int geom(double p)
Returns a sample from a Geometric distribution Discrete distribution with decay rate p Mean = (1-p) / p Probability mass function for each integer output k = p.(1-p)^k
-
nextInt
public static final int nextInt()
Sample a random signed 32-bit integer- Returns:
-
nextShort
public static final short nextShort()
Sample a random signed short value- Returns:
-
nextChar
public static final char nextChar()
Sample a random char- Returns:
-
nextLetterString
public static final java.lang.String nextLetterString(int length)
-
nextByte
public static final byte nextByte()
Sample a random (signed) byte- Returns:
-
nextBoolean
public static boolean nextBoolean()
Sample a random boolean value with 50% chance- Returns:
-
nextLetter
public static final char nextLetter()
Sample a random lowercase letter- Returns:
-
r
public static final int r(int s)
Random number from zero to s-1- Parameters:
s- Upper bound (excluded)- Returns:
-
otherIndex
public static final int otherIndex(int i, int max)Return a random index between 0 and max (exclusive) not equal to i
-
nextDouble
public static final double nextDouble()
Returns standard double in range [0..1)- Returns:
-
nextFloat
public static final float nextFloat()
Sample a random float in range [0..1)- Returns:
-
u
public static final double u()
Sample a random uniform double in range [0..1)- Returns:
-
u
public static final double u(double max)
Sample a random uniform double in range [0..max)- Parameters:
max-- Returns:
-
u
public static final double u(double min, double max)Sample a random uniform double in the range [min..max)- Parameters:
min-max-- Returns:
-
round
public static final int round(double d)
Randomly rounds to the nearest integer
-
range
public static final int range(int n1, int n2)Returns random number uniformly distributed in inclusive [n1, n2] range. It is allowed to have to n1 > n2, or n1 < n2, or n1 == n2.
-
d
public static final int d(int sides)
simulates a dice roll with the given number of sides- Parameters:
sides-- Returns:
-
d3
public static final int d3()
-
d4
public static final int d4()
-
d6
public static final int d6()
-
d8
public static final int d8()
-
d10
public static final int d10()
-
d12
public static final int d12()
-
d20
public static final int d20()
-
d100
public static final int d100()
-
n
public static double n(double u, double sd)Generates a normal distributed number with the given mean and standard deviation- Parameters:
u- Meansd- Standard deviation- Returns:
-
nextGaussian
public static double nextGaussian()
-
nextString
public static java.lang.String nextString()
-
shuffle
public static <T> void shuffle(T[] ts)
Randomly shuffles all elements in a given array
-
chooseIntegers
public static void chooseIntegers(int[] dest, int destOffset, int length, int maxValue)Chooses a set of distinct integers from a range 0 to maxValue-1 Resulting integers are sorted
-
randIntegers
public static void randIntegers(int[] dest, int destOffset, int length, int maxValue)Creates a set of n random integers from a range of 0 to maxValue-1
-
pick
public static <T> T pick(T[] ts)
Picks a random item from a given array
-
pick
public static <T> T pick(java.util.List<T> ts)
Picks a random item from a given list
-
pick
public static <T> T pick(java.util.Collection<T> ts)
Picks a random item from a given collection
-
fillUniform
public static void fillUniform(float[] d, int start, int length)
-
fillBinary
public static void fillBinary(float[] d, int start, int length)
-
fillGaussian
public static void fillGaussian(float[] d, int start, int length, float u, float sd)
-
fillGaussian
public static void fillGaussian(double[] d, int start, int length, double u, double sd)
-
fillBinary
public static void fillBinary(double[] data, int start, int length, double mean)
-
binary
public static double binary(double mean)
-
binarySample
public static void binarySample(float[] temp, int offset, int length)
-
binarySample
public static void binarySample(double[] temp, int offset, int length)Randomises the temp array to 0.0 or 1.0 according to the probabilities in the array- Parameters:
temp-offset-length-
-
indexFromWeights
public static int indexFromWeights(double[] probabilities)
-
-
DMelt 3.0 © DataMelt by jWork.ORG