Random is located under the java.util package and generates Random numbers
Random number:
Java.util.random Random = new Random(); Int num1 = random.nextint (); int num1 = random.nextint (); System.out.println(num1); Int num2 = random. NextInt (101); // Generate a random number between [0 and 100]. // 101 system.out.println (num2);Copy the code
Generate 5 different random numbers in an array:
Random random = new Random(); int[] array = new int[5]; For (int I = 0; i < array.length; i++){ array[i] = -1; } // array index = 0; while (index < array.length){ //int num = random.nextInt(101); int num = random.nextInt(6); if (! contains(array,num)){ array[index++] = num; For (int I = 0; i < array.length; i++){ System.out.println(array[i]); }} public static Boolean contains(int[] array, int key){for (int I = 0; i < array.length; i++){ if (array[i] == key){ return true; } } return false;Copy the code