Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.security.SecureRandom;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.Collection;
- import java.util.Collections;
- import java.util.Iterator;
- import java.util.List;
- import java.util.Map;
- import java.util.Queue;
- import java.util.Random;
- import java.util.Set;
- /**
- * Class for all randoms
- *
- * @author Emre
- *
- */
- @SuppressWarnings("unused")
- public class Randoms {
- /** The random of this class */
- private static final Random RANDOM = new Random();
- /** The secure random of this class */
- private static final SecureRandom SECURE_RANDOM = new SecureRandom();
- /**
- * A random
- *
- * @param min
- * The minimum amount
- *
- * @param max
- * The maximum amount
- *
- * @return
- * min - max
- */
- public static int randomInclusive(int min, int max) {
- return Math.min(min, max) + SECURE_RANDOM.nextInt(Math.max(min, max) - Math.min(min, max) + 1);
- }
- /**
- * Another random with the minimum of 0
- *
- * @param max
- * The maximum
- *
- * @return
- * 0 - max
- */
- public static int randomInclusive(int max) {
- return randomInclusive(0, max);
- }
- /**
- * A random exclusive the max
- *
- * @param min
- * The minimum amount
- *
- * @param max
- * The maximum amount
- *
- * @return
- * min - max
- */
- public static int randomExclusive(int min, int max) {
- return Math.min(min, max) + SECURE_RANDOM.nextInt(Math.max(min, max) - Math.min(min, max));
- }
- /**
- * Another random with the minimum of 0 and exclusive the max
- *
- * @param max
- * The maximum
- *
- * @return
- * 0 - max
- */
- public static int randomExclusive(int max) {
- return randomExclusive(0, max);
- }
- /**
- * Picks a random int of an int array
- *
- * @param random
- * The array
- *
- * @return A random from an array
- */
- public static int randomIntegerOfArray(int[] random) {
- return random[SECURE_RANDOM.nextInt(random.length)];
- }
- /**
- * Picks a random int of an int array
- *
- * @param random
- * The array
- *
- * @return A random from an array
- */
- public static int randomIntegerOfIntegers(int... random) {
- return random[SECURE_RANDOM.nextInt(random.length)];
- }
- /**
- * Picks a random string of a string array
- *
- * @param random
- * The array
- *
- * @return A random fron an string array
- */
- public static String randomStringOfArray(String[] random) {
- return random[SECURE_RANDOM.nextInt(random.length)];
- }
- /**
- * Picks a random string of a string array
- *
- * @param random
- * The array
- *
- * @return A random fron an string array
- */
- public static String randomStringOfStrings(String... random) {
- return random[SECURE_RANDOM.nextInt(random.length)];
- }
- /**
- * Picks a random double of a double array
- *
- * @param random
- * The array
- *
- * @return A random from an array of doubles
- */
- public static double randomDoubleOfArray(double[] random) {
- return random[SECURE_RANDOM.nextInt(random.length)];
- }
- /**
- * Picks a random double of a double array
- *
- * @param random
- * The array
- *
- * @return A random from an array of doubles
- */
- public static double randomDoubleOfDoubles(double... random) {
- return random[SECURE_RANDOM.nextInt(random.length)];
- }
- /**
- * Picks a random float of a float array
- *
- * @param random
- * The array
- *
- * @return A random from an array of floats
- */
- public static float randomFloatOfArray(float[] random) {
- return random[SECURE_RANDOM.nextInt(random.length)];
- }
- /**
- * Picks a random float of a float array
- *
- * @param random
- * The array
- *
- * @return A random from an array of floats
- */
- public static float randomFloatOfFloats(float... random) {
- return random[SECURE_RANDOM.nextInt(random.length)];
- }
- /**
- * Picks a random char of a char array
- *
- * @param random
- * The array
- *
- * @return A random from an array of chars
- */
- public static char randomCharOfArray(char[] random) {
- return random[SECURE_RANDOM.nextInt(random.length)];
- }
- /**
- * Picks a random char of a char array
- *
- * @param random
- * The array
- *
- * @return A random from an array of chars
- */
- public static char randomCharOfChars(char... random) {
- return random[SECURE_RANDOM.nextInt(random.length)];
- }
- /**
- * Picks a random boolean of a boolean array
- *
- * @param random
- * The array
- *
- * @return A random from an array of booleans
- */
- public static boolean randomBooleanOfArray(boolean[] random) {
- return random[SECURE_RANDOM.nextInt(random.length)];
- }
- /**
- * Picks a random boolean of a boolean array
- *
- * @param random
- * The array
- *
- * @return A random from an array of booleans
- */
- public static boolean randomBooleanOfBooleans(boolean... random) {
- return random[SECURE_RANDOM.nextInt(random.length)];
- }
- /**
- * Picks a random long of a long array
- *
- * @param random
- * The array
- *
- * @return A random from an array of longs
- */
- public static long randomLongOfArray(long[] random) {
- return random[SECURE_RANDOM.nextInt(random.length)];
- }
- /**
- * Picks a random long of a long array
- *
- * @param random
- * The array
- *
- * @return A random from an array of longs
- */
- public static long randomLongOfLongs(long... random) {
- return random[SECURE_RANDOM.nextInt(random.length)];
- }
- /**
- * Picks a random byte from a byte array
- *
- * @param random
- * The array
- *
- * @return A random from an array of bytes
- */
- public static byte randomByteOfArray(byte[] random) {
- return random[SECURE_RANDOM.nextInt(random.length)];
- }
- /**
- * Picks a random byte from a byte array
- *
- * @param random
- * The array
- *
- * @return A random from an array of bytes
- */
- public static byte randomByteOfBytes(byte... random) {
- return random[SECURE_RANDOM.nextInt(random.length)];
- }
- /**
- * Picks a random short from a shot array
- *
- * @param random
- * The array
- *
- * @return A random from an array of shorts
- */
- public static short randomShortOfArray(short[] random) {
- return random[SECURE_RANDOM.nextInt(random.length)];
- }
- /**
- * Picks a random short from a shot array
- *
- * @param random
- * The array
- *
- * @return A random from an array of shorts
- */
- public static short randomShortOfShorts(short... random) {
- return random[SECURE_RANDOM.nextInt(random.length)];
- }
- /**
- * Picks a random type from a list
- *
- * @param list
- * The list
- *
- * @return A random type from a list
- */
- public static <T> T randomTypeOfList(List<T> list) {
- Collections.shuffle(list);
- return list.get(SECURE_RANDOM.nextInt(list.size()));
- }
- /**
- * Picks a random type of a set
- *
- * @param set
- * The set
- *
- * @return A random from a set
- */
- public static <T> T randomTypeOfSet(Set<T> set) {
- ArrayList<T> list = new ArrayList<>(set);
- Collections.shuffle(list);
- return list.get(SECURE_RANDOM.nextInt(set.size()));
- }
- /**
- * Picks a random key from a map
- *
- * @param map
- * The map
- *
- * @return A random key from a map
- */
- public static <K, V> K randomKeyOfMap(Map<K, V> map) {
- ArrayList<K> list = new ArrayList<>(map.keySet());
- Collections.shuffle(list);
- return list.get(SECURE_RANDOM.nextInt(list.size()));
- }
- /**
- * Picks a random value from a map
- *
- * @param map
- * The map
- *
- * @return A random value from a map
- *
- */
- public static <K,V> V randomValueOfMap(Map<K,V> map) {
- ArrayList<V> list = new ArrayList<>(map.values());
- Collections.shuffle(list);
- return list.get(SECURE_RANDOM.nextInt(list.size()));
- }
- /**
- * Picks a random element of a queue
- *
- * @param RandomNumbers
- * The queue
- *
- * @return A random element from a queue
- */
- public static <E> E randomElementOfQueue(Queue<E> queue) {
- ArrayList<E> list = new ArrayList<>(queue);
- Collections.shuffle(list);
- return list.get(SECURE_RANDOM.nextInt(queue.size()));
- }
- /**
- * Picks a random from any type array
- *
- * @param array
- * The array
- *
- * @return A random type from a type array
- */
- public static <T> T randomTypeOfAllArrays(T[] array) {
- List<T> list = new ArrayList<>(Arrays.asList(array));
- Collections.shuffle(list);
- return list.get(SECURE_RANDOM.nextInt(list.size()));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement