Advertisement
Guest User

Untitled

a guest
Feb 7th, 2016
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.41 KB | None | 0 0
  1. import java.security.SecureRandom;
  2. import java.util.ArrayList;
  3. import java.util.Arrays;
  4. import java.util.Collection;
  5. import java.util.Collections;
  6. import java.util.Iterator;
  7. import java.util.List;
  8. import java.util.Map;
  9. import java.util.Queue;
  10. import java.util.Random;
  11. import java.util.Set;
  12.  
  13. /**
  14.  * Class for all randoms
  15.  *
  16.  * @author Emre
  17.  *
  18.  */
  19. @SuppressWarnings("unused")
  20. public class Randoms {
  21.    
  22.     /** The random of this class */
  23.     private static final Random RANDOM = new Random();
  24.  
  25.     /** The secure random of this class */
  26.     private static final SecureRandom SECURE_RANDOM = new SecureRandom();
  27.    
  28.     /**
  29.      * A random
  30.      *
  31.      * @param min
  32.      *          The minimum amount
  33.      *
  34.      * @param max
  35.      *          The maximum amount
  36.      *
  37.      * @return
  38.      *          min - max
  39.      */
  40.     public static int randomInclusive(int min, int max) {
  41.         return Math.min(min, max) + SECURE_RANDOM.nextInt(Math.max(min, max) - Math.min(min, max) + 1);
  42.     }
  43.    
  44.     /**
  45.      * Another random with the minimum of 0
  46.      *
  47.      * @param max
  48.      *          The maximum
  49.      *
  50.      * @return
  51.      *          0 - max
  52.      */
  53.     public static int randomInclusive(int max) {
  54.         return randomInclusive(0, max);
  55.     }
  56.    
  57.     /**
  58.      * A random exclusive the max
  59.      *
  60.      * @param min
  61.      *          The minimum amount
  62.      *
  63.      * @param max
  64.      *          The maximum amount
  65.      *
  66.      * @return
  67.      *          min - max
  68.      */
  69.     public static int randomExclusive(int min, int max) {
  70.         return Math.min(min, max) + SECURE_RANDOM.nextInt(Math.max(min, max) - Math.min(min, max));
  71.     }
  72.    
  73.     /**
  74.      * Another random with the minimum of 0 and exclusive the max
  75.      *
  76.      * @param max
  77.      *          The maximum
  78.      *
  79.      * @return
  80.      *          0 - max
  81.      */
  82.     public static int randomExclusive(int max) {
  83.         return randomExclusive(0, max);
  84.     }
  85.    
  86.     /**
  87.      * Picks a random int of an int array
  88.      *
  89.      * @param random
  90.      *          The array
  91.      *
  92.      * @return A random from an array
  93.      */
  94.     public static int randomIntegerOfArray(int[] random) {
  95.         return random[SECURE_RANDOM.nextInt(random.length)];
  96.     }
  97.  
  98.     /**
  99.      * Picks a random int of an int array
  100.      *
  101.      * @param random
  102.      *          The array
  103.      *
  104.      * @return A random from an array
  105.      */
  106.     public static int randomIntegerOfIntegers(int... random) {
  107.         return random[SECURE_RANDOM.nextInt(random.length)];
  108.     }
  109.  
  110.     /**
  111.      * Picks a random string of a string array
  112.      *
  113.      * @param random
  114.      *          The array
  115.      *
  116.      * @return A random fron an string array
  117.      */
  118.     public static String randomStringOfArray(String[] random) {
  119.         return random[SECURE_RANDOM.nextInt(random.length)];
  120.     }
  121.  
  122.  
  123.     /**
  124.      * Picks a random string of a string array
  125.      *
  126.      * @param random
  127.      *          The array
  128.      *
  129.      * @return A random fron an string array
  130.      */
  131.     public static String randomStringOfStrings(String... random) {
  132.         return random[SECURE_RANDOM.nextInt(random.length)];
  133.     }
  134.  
  135.     /**
  136.      * Picks a random double of a double array
  137.      *
  138.      * @param random
  139.      *          The array
  140.      *
  141.      * @return A random from an array of doubles
  142.      */
  143.     public static double randomDoubleOfArray(double[] random) {
  144.         return random[SECURE_RANDOM.nextInt(random.length)];
  145.     }
  146.    
  147.     /**
  148.      * Picks a random double of a double array
  149.      *
  150.      * @param random
  151.      *          The array
  152.      *
  153.      * @return A random from an array of doubles
  154.      */
  155.     public static double randomDoubleOfDoubles(double... random) {
  156.         return random[SECURE_RANDOM.nextInt(random.length)];
  157.     }
  158.    
  159.     /**
  160.      * Picks a random float of a float array
  161.      *
  162.      * @param random
  163.      *          The array
  164.      *
  165.      * @return A random from an array of floats
  166.      */
  167.     public static float randomFloatOfArray(float[] random) {
  168.         return random[SECURE_RANDOM.nextInt(random.length)];
  169.     }
  170.  
  171.     /**
  172.      * Picks a random float of a float array
  173.      *
  174.      * @param random
  175.      *          The array
  176.      *
  177.      * @return A random from an array of floats
  178.      */
  179.     public static float randomFloatOfFloats(float... random) {
  180.         return random[SECURE_RANDOM.nextInt(random.length)];
  181.     }
  182.  
  183.     /**
  184.      * Picks a random char of a char array
  185.      *
  186.      * @param random
  187.      *          The array
  188.      *
  189.      * @return A random from an array of chars
  190.      */
  191.     public static char randomCharOfArray(char[] random) {
  192.         return random[SECURE_RANDOM.nextInt(random.length)];
  193.     }
  194.  
  195.     /**
  196.      * Picks a random char of a char array
  197.      *
  198.      * @param random
  199.      *          The array
  200.      *
  201.      * @return A random from an array of chars
  202.      */
  203.     public static char randomCharOfChars(char... random) {
  204.         return random[SECURE_RANDOM.nextInt(random.length)];
  205.     }
  206.  
  207.     /**
  208.      * Picks a random boolean of a boolean array
  209.      *
  210.      * @param random
  211.      *          The array
  212.      *
  213.      * @return A random from an array of booleans
  214.      */
  215.     public static boolean randomBooleanOfArray(boolean[] random) {
  216.         return random[SECURE_RANDOM.nextInt(random.length)];
  217.     }
  218.    
  219.     /**
  220.      * Picks a random boolean of a boolean array
  221.      *
  222.      * @param random
  223.      *          The array
  224.      *
  225.      * @return A random from an array of booleans
  226.      */
  227.     public static boolean randomBooleanOfBooleans(boolean... random) {
  228.         return random[SECURE_RANDOM.nextInt(random.length)];
  229.     }
  230.  
  231.     /**
  232.      * Picks a random long of a long array
  233.      *
  234.      * @param random
  235.      *          The array
  236.      *
  237.      * @return A random from an array of longs
  238.      */
  239.     public static long randomLongOfArray(long[] random) {
  240.         return random[SECURE_RANDOM.nextInt(random.length)];
  241.     }
  242.  
  243.     /**
  244.      * Picks a random long of a long array
  245.      *
  246.      * @param random
  247.      *          The array
  248.      *
  249.      * @return A random from an array of longs
  250.      */
  251.     public static long randomLongOfLongs(long... random) {
  252.         return random[SECURE_RANDOM.nextInt(random.length)];
  253.     }
  254.  
  255.     /**
  256.      * Picks a random byte from a byte array
  257.      *
  258.      * @param random
  259.      *          The array
  260.      *  
  261.      * @return A random from an array of bytes
  262.      */
  263.     public static byte randomByteOfArray(byte[] random) {
  264.         return random[SECURE_RANDOM.nextInt(random.length)];
  265.     }
  266.  
  267.     /**
  268.      * Picks a random byte from a byte array
  269.      *
  270.      * @param random
  271.      *          The array
  272.      *  
  273.      * @return A random from an array of bytes
  274.      */
  275.     public static byte randomByteOfBytes(byte... random) {
  276.         return random[SECURE_RANDOM.nextInt(random.length)];
  277.     }
  278.  
  279.     /**
  280.      * Picks a random short from a shot array
  281.      *
  282.      * @param random
  283.      *          The array
  284.      *
  285.      * @return A random from an array of shorts
  286.      */
  287.     public static short randomShortOfArray(short[] random) {
  288.         return random[SECURE_RANDOM.nextInt(random.length)];
  289.     }
  290.  
  291.     /**
  292.      * Picks a random short from a shot array
  293.      *
  294.      * @param random
  295.      *          The array
  296.      *
  297.      * @return A random from an array of shorts
  298.      */
  299.     public static short randomShortOfShorts(short... random) {
  300.         return random[SECURE_RANDOM.nextInt(random.length)];
  301.     }
  302.  
  303.     /**
  304.      * Picks a random type from a list
  305.      *
  306.      * @param list
  307.      *          The list
  308.      *
  309.      * @return A random type from a list
  310.      */
  311.     public static <T> T randomTypeOfList(List<T> list) {
  312.         Collections.shuffle(list);
  313.         return list.get(SECURE_RANDOM.nextInt(list.size()));
  314.     }
  315.    
  316.     /**
  317.      * Picks a random type of a set
  318.      *
  319.      * @param set
  320.      *          The set
  321.      *
  322.      * @return A random from a set
  323.      */
  324.     public static <T> T randomTypeOfSet(Set<T> set) {
  325.         ArrayList<T> list = new ArrayList<>(set);
  326.         Collections.shuffle(list);
  327.         return list.get(SECURE_RANDOM.nextInt(set.size()));
  328.     }
  329.    
  330.     /**
  331.      * Picks a random key from a map
  332.      *
  333.      * @param map
  334.      *          The map
  335.      *
  336.      * @return A random key from a map
  337.      */
  338.     public static <K, V> K randomKeyOfMap(Map<K, V> map) {
  339.         ArrayList<K> list = new ArrayList<>(map.keySet());
  340.         Collections.shuffle(list);
  341.         return list.get(SECURE_RANDOM.nextInt(list.size()));
  342.     }
  343.    
  344.     /**
  345.      * Picks a random value from a map
  346.      *
  347.      * @param map
  348.      *          The map
  349.      *
  350.      * @return A random value from a map
  351.      *
  352.      */
  353.     public static <K,V> V randomValueOfMap(Map<K,V> map) {
  354.         ArrayList<V> list = new ArrayList<>(map.values());
  355.         Collections.shuffle(list);
  356.         return list.get(SECURE_RANDOM.nextInt(list.size()));
  357.     }
  358.    
  359.     /**
  360.      * Picks a random element of a queue
  361.      *
  362.      * @param RandomNumbers
  363.      *          The queue
  364.      *
  365.      * @return A random element from a queue
  366.      */
  367.     public static <E> E randomElementOfQueue(Queue<E> queue) {
  368.         ArrayList<E> list = new ArrayList<>(queue);
  369.         Collections.shuffle(list);
  370.         return list.get(SECURE_RANDOM.nextInt(queue.size()));
  371.     }
  372.    
  373.     /**
  374.      * Picks a random from any type array
  375.      *
  376.      * @param array
  377.      *          The array
  378.      *
  379.      * @return A random type from a type array
  380.      */
  381.     public static <T> T randomTypeOfAllArrays(T[] array) {
  382.         List<T> list = new ArrayList<>(Arrays.asList(array));
  383.         Collections.shuffle(list);
  384.         return list.get(SECURE_RANDOM.nextInt(list.size()));
  385.     }
  386. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement