Pseudorandom Number Generator - UQdeco2800/2022-studio-3 GitHub Wiki
Overview
PseudoRandom class provides an API for seeding Pseduo-Generated Random features.
Usage
Generating a pseudo-randomly number
Returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0. Returned values are chosen pseudorandomly with (approximately) uniform distribution from that range. When this method is first called, it creates a single new pseudorandom-number generator, exactly as if by the expression.
public static double randomUnitNumberGenerator () {
return random();
}
Generating pseudo-randomly integers or floating point number
Returns a pseudorandomly chosen double or integer value between a specifed lowerBound and upperBound.
public static double seedRandomDouble (double lowerBound, double upperBound) {
return lowerBound + new Random().nextDouble(upperBound - lowerBound);
}
public static int seedRandomInt (int lowerBound, int upperBound) {
return lowerBound + new Random().nextInt(upperBound - lowerBound);
}
Note: lowerBound and upperBound must be positive and finite, lowerBound must be less than upperBound.
Randomly selecting an item from a list
Selectes an item from a list by pseudo-randomly generating an integer between 0 and the length of the list, and returning the element at this index.
public static <T> T getRandomItem(List<T> items) {
int lenItems = items.size();
int i = seedRandomInt(0, lenItems);
return items.get(i);
}