1. How to generate random numbers in JAVA
1. Use Math.random() in j2se to make the system randomly select a double type decimal between 0 and 1, multiply it by a number, such as 25, and you can get a random number in the range of 0 and 25. This is not available in j2me;
int randomNumber = (int) Math.round(Math.random()*(max-min)+min);
2. There is a currentTimeMillis() method in the System class. This method returns a long millisecond number from 0:00 on January 1, 1970 to the current one. It can be used as a random number, and it can also be moduloed to certain numbers, which can limit the range of random numbers. When this method generates multiple random numbers at the same time in a loop, it will be the same value, which has certain limitations!
long randomNum = System.currentTimeMillis(); int randomNumber = (int) randomNum%(max-min)+min;
3. Use the java.util.Random class to generate a random number generator, which is also a method we often use to get random numbers in j2me programs. It has two forms of constructors, namely Random() and Random(long seed). Random() uses the current time, System.currentTimeMillis(), as the seed of the generator, and Random(long seed) uses the specified seed as the seed of the generator. After the random number generator (Random) object is generated, different types of random numbers are obtained by calling different methods: nextInt(), nextLong(), nextFloat(), nextDouble(), etc. If two Random objects use the same seed (for example, both are 25) and call the same function in the same order, they return exactly the same value.
Random random = new Random(); int randomNumber = random.nextInt(max)%(max-min+1) + min;
2. N non-repetitive numbers within a random given range
1. Method 1: The simplest and most easy to understand two cycles to remove heavy weight
/** * Randomly specify N non-repetitive numbers in the range* The simplest and most basic method* @param min Specify the minimum range* @param max Specify the maximum range* @param n Number of random numbers*/ public static int[] randomCommon(int min, int max, int n){ if (n > (max - min + 1) || max < min) { return null; } int[] result = new int[n]; int count = 0; while(count < n) { int num = (int) (Math.random() * (max - min)) + min; boolean flag = true; for (int j = 0; j < n; j++) { if(num == result[j]){ flag = false; break; } } if(flag){ result[count] = num; count++; } } return result; } 2. Method 2: Using the features of HashSet, only different values can be stored
/** * Randomly specify N non-repetitive numbers in the range* Using the HashSet feature, only different values can be stored* @param min Specify the minimum value of the range* @param max Specify the maximum value of the range* @param n Random number* @param HashSet<Integer> set Random number result set*/ public static void randomSet(int min, int max, int n, HashSet<Integer> set) { if (n > (max - min + 1) || max < min) { return; } for (int i = 0; i < n; i++) { // Call the Math.random() method int num = (int) (Math.random() * (max - min)) + min; set.add(num);//Save different numbers into HashSet} int setSize = set.size(); // If the stored number is less than the specified number, then recursively generates the random number of the remaining number, and loop so that the specified size is reached if (setSize < n) { randomSet(min, max, n - setSize, set);// Recursive} } 3. Method 3: Exclude randomly arrived numbers
/** * Randomly specify N non-repetitive numbers in the range* In the initialized unrepetitive array, a number is generated randomly and put into the result. * Replace the number that is randomly sent to the array to be selected by the number corresponding to the subscript of the array to be selected (len-1)* Then randomly generate the next random number from len-2, and so on* @param max Specify the maximum range value* @param min Specify the minimum range value* @param n Number of random numbers* @return int[] Random number result set*/ public static int[] randomArray(int min,int max,int n){ int len = max-min+1; if(max < min || n > len){ return null; } //Initialize the array to be selected for a given range int[] source = new int[len]; for (int i = min; i < min+len; i++){ source[i-min] = i; } int[] result = new int[n]; Random rd = new Random(); int index = 0; for (int i = 0; i < result.length; i++) { //Array to be selected 0 to (len-2) random index = Math.abs(rd.nextInt() % len--); //Put the random number into the result set result[i] = source[index]; //Replace the number randomly received in the array to be selected with the number corresponding to the subscript of the array to be selected (len-1) with the number corresponding to the subscript of the array to be selected (len-1). source[index] = source[len]; } return result; } Calling instance:
public static void main(String[] args) { int[] result1 = randomCommon(20,50,10); for (int i : result1) { System.out.println(i); } int[] result2 = randomArray(20,50,10); for (int i : result2) { System.out.println(i); } HashSet<Integer> set = new HashSet<Integer>(); randomSet(20,50,10,set); for (int j : set) { System.out.println(j); } }3. Sample code
package test;import java.util.HashSet;import java.util.Random;public class Snippet { /** * Randomly specify N non-repetition numbers in the range* In the initialized unrepetitioned array, randomly generate a number and put it into the result, * Replace the number that is randomly sent to the array to be randomly sent by the number corresponding to the subscript of the array to be selected (len-1)* Then randomly generate the next random number from len-2, and so on* @param max Specify the maximum range* @param min Specify the minimum range* @param n Number of random numbers* @return int[] Random number result set*/ public static int[] randomArray(int min,int max,int n){ int len = max-min+1; if(max < min || n > len){ return null; } //Initialize the array to be selected for a given range int[] source = new int[len]; for (int i = min; i < min+len; i++){ source[i-min] = i; } int[] result = new int[n]; Random rd = new Random(); int index = 0; for (int i = 0; i < result.length; i++) { //Array to be selected 0 to (len-2) a random subscript int s=rd.nextInt()%len; // System.out.print(s-- +","); index = Math.abs(rd.nextInt()%len--);// System.out.println(index); //Put the randomly received number into the result set result[i] = source[index]; //Replace the randomly received number in the array to be selected with the corresponding number of the subscript of the array to be selected (len-1) source[index] = source[len]; } return result; } public static void main(String[] args) {// int[] result1 = randomCommon(20,50,10);// for (int i : result1) {// System.out.println(i);// } int[] result2 = randomArray(0,4,5); for (int i : result2) { System.out.print(i); } // HashSet<Integer> set = new HashSet<Integer>();// randomSet(20,50,10,set);// for (int j : set) {// System.out.println(j);// } }}The above is all about this article, I hope it will be helpful for everyone to learn Java programming.