Java program realizes the method summary of generating random Numbers from random N non repeating Numbers in a given range

  • 2020-06-23 00:30:34
  • OfStack

This article illustrates the Java programming method to generate random Numbers from random N non-repeating Numbers in a given range. To share for your reference, specific as follows:

1. Methods for generating random Numbers in JAVA

1. Use ES10en. random() in j2se to make the system randomly select a decimal of type double between 0 and 1 and multiply it by a number, such as 25, to get a random number within the range of 0 to 25, which is not available in j2me;


int randomNumber = (int) Math.round(Math.random()*(max-min)+min);

2. In the System class, there is an currentTimeMillis() method, which returns the number of milliseconds from 0:00 0min 0sec on January 1, 1970 to the current long type. This method can be used as a random number, and it can also be used as a modulus for some Numbers to limit the range of random Numbers. This method in the loop at the same time to generate more than one random number, will be the same value, there is a definite limitation!


long randomNum = System.currentTimeMillis();
int randomNumber = (int) randomNum%(max-min)+min;

3. Use the class ES24en.util.Random to generate a random number generator, which is also the method we often use to select a random number in the program of j2me. It has two forms of constructors, Random() and Random(long seed). Random() USES the current time System.currentTimeMillis () as the generator seed, and Random(long seed) USES the specified seed as the generator seed. After the random number generator (Random) object is generated, different types of random Numbers are obtained by calling different method: nextInt(), nextLong(), nextFloat(), nextDouble(), etc. If two Random objects use the same seed (say, 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-repeating Numbers within a random given range

1. Method 1: The simplest and most easily understood double-cycle weight loss


/**
 *  Randomly assigned range N A number that doesn't repeat 
 *  The simplest and most basic way 
 * @param min  Specifies the range minimum 
 * @param max  Specified range maximum 
 * @param n  Random number 
*/
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 characteristics of HashSet, only different values can be stored


/**
 *  Randomly assigned range N A number that doesn't repeat 
 *  using HashSet Can only store different values 
 * @param min  Specifies the range minimum 
 * @param max  Specified range maximum 
 * @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 Math.random() methods 
      int num = (int) (Math.random() * (max - min)) + min;
      set.add(num);//  I'm going to store different Numbers HashSet In the 
    }
    int setSize = set.size();
    //  If the number stored is less than the specified number generated, the call recursively generates a random number of the remaining number, and so on, until the specified size is reached 
    if (setSize < n) {
    randomSet(min, max, n - setSize, set);//  recursive 
    }
}

3. Method 3: Exclude the random Numbers


/**
 *  Randomly assigned range N A number that doesn't repeat 
 *  Randomly generated in the initialized non-repeat pending array 1 You put a number in the result, 
 *  The number to which the array to be selected is randomly assigned. Use the array to be selected (len-1) The substitution of the subscript 
 *  Then from len-2 Random generation in the next 1 Random Numbers, and so on 
 * @param max  Specified range maximum 
 * @param min  Specifies the range minimum 
 * @param n  Random number 
 * @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;
  }
  // Initializes an array of options 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++) {
    // To choose an array 0 to (len-2) random 1 A subscript 
      index = Math.abs(rd.nextInt() % len--);
      // Put the random Numbers into the result set 
      result[i] = source[index];
      // The number randomly assigned to an array to be selected, using the array to be selected (len-1) The substitution of the subscript 
      source[index] = source[len];
    }
    return result;
}

Call instance:


public static void main(String[] args) {
  int[] reult1 = randomCommon(20,50,10);
  for (int i : reult1) {
    System.out.println(i);
  }
  int[] reult2 = randomArray(20,50,10);
  for (int i : reult2) {
    System.out.println(i);
  }
  HashSet<Integer> set = new HashSet<Integer>();
  randomSet(20,50,10,set);
    for (int j : set) {
    System.out.println(j);
  }
}

PS: Here are two more online tools you can use:

Online random number/string generation tool:
http://tools.ofstack.com/aideddesign/suijishu

High strength password generator:
http://tools.ofstack.com/password/CreateStrongPassword

I hope this article has been helpful for java programming.


Related articles: