A simple alternative to java's random. Inspired in python's simple usability.
You should consider the array and noElements as follows:
array = [a, b, c, d, e, null, null]
length = 7
noElements = 5
As you can see, the array is of size 7, however noElements is of size 5 since there are only 5 elements, also, all the elements must be concentrated in the left part of the array without null spaces. Otherwise, you will get various errors during execution.
(Ranrandom.random(double min, double max) -> double)
Returns a random number between the given range (greater than or equal to the min and less than the max).
min: 10, max: 20 -> 10 to 19.999
(Ranrandom.random(double top) -> double)
Returns a random number greater than or equal to cero and less than the top.
top: 10 -> 0 to 9.999
(Ranrandom.randInt(int min, double max) -> int)
Returns a random integer between the given range (greater than or equal to the min and less than the max).
min: 5, max: 35 -> 5 to 34
(Ranrandom.randInt(int top) -> int)
Returns a random integer greater than or equal to cero and less than the top.
top: 10 -> 0 to 9
(Ranrandom.choice(T[] array, int noElements) -> T)
Returns a random value from a given array (from indexes 0 to noElements - 1).
array: [a, b, c, null], noElements: 3 -> c
(Ranrandom.choice(Iterable<T> iterable) -> T)
Returns a random value from a given Iterable.
iterable: {a, b, c} -> b
(Ranrandom.choice(T... values) -> T)
Returns a random value from values.
values: a, b, c, d, e -> d
values: [a, b, c, d, e] -> b
(Ranrandom.choices(T[] array, int noElements) -> T[])
Returns an array with a random selection from the given array, with a random size from 1 to noElements -1 (array must contain at least 2 elements).
array: [a, b, c, d], noElements: 4 -> [a, d]
(Ranrandom.choices(int length, T[] array, int noElements) -> T[])
Returns an array with a random selection from the given array, with length size (array must contain at least 2 elements and length must be less than noElements).
length: 3, array: [a, b, c, d], noElements: 4 -> [a, c, d]
(Ranrandom.choices(Iterable<T> iterable) -> T[])
Returns an array with a random selection from the given iterable, with a random size from 1 to iterable's size - 1 (iterable must contain at least 2 elements).
iterable: {a, b, c, d} -> [b, c, d]
(Ranrandom.choices(int length, Iterable<T> iterable) -> T[])
Returns an array with a random selection from the given Iterable, with length size (array must contain at least 2 elements and length must be less than iterable's length).
length: 3, iterable: {a, b, c, d} -> [c, a, b]
(Ranrandom.choices(T... values) -> T[])
Returns an array with a random selection from the given values, with a random size from 1 to value's size - 1 (values must contain at least 2 elements).
values: a, b, c, d, e -> [e, b, d]
values: [a, b, c, d, e] -> [a, d, c]
(Ranrandom.choicesLength(int length, T... values) -> T[])
Returns an array with a random selection from the given values, with a random size from 1 to value's size - 1 (values must contain at least 2 elements and length must be less than value's length).
length: 2, values: a, b, c, d, e -> [c, a]
length: 4, values: [a, b, c, d, e] -> [c, b, e, d]
(Ranrandom.shuffle(T[] array, int noElementos) -> void)
Return the array in random order (This method changes the original array, it does not return a new array.)
array: [a, b, c, d, null], noElements: 4 => [b, d, a, c, null]
(Ranrandom.shuffle(ArrayList arrayList) -> void)
Return the ArrayList in random order (This method changes the original ArrayList, it does not return a new ArrayList.)
arrayList: {a, b, c, d} => {c, b, d, a}
The RandStack object allows you to create a copy of an array, an Iterator or n values, and empty them in random order.
RandStack(T[] array, int noElements)
You can construct a RandStack object with an array and the number of elements in it.
RandStack(Iterable<T> iterable)
You can construct a RandStack object with an Iterator.
RandStack(T... values)
You can construct a RandStack object with n values.
Pop
(RandStack.pop() -> T)
Returns a random value stored in the RandStack and deletes it.
Is Empty
(RandStack.isEmpty() -> boolean)
Returns true if the RandStack is empty.
Size
(RandStack.size() -> int)
Returns the number of elements left in the RandStack.
Get Stored Class
(RandStack.getStoredClass() -> Class)
Returns the class of the elements stored in the RandStack if not Empty.
RandStack<Type> randStack = new RandStack<Type>(Constructor);
while (!randStack.isEmpty())
System.out.println(randStack.pop());