Implement random number free methods in Java

  • 2020-04-01 01:36:26
  • OfStack

The friend that has a bit of development experience commonly can realize such function, it is the problem on efficiency only. Generally, when we are faced with such problems, we always think of it as an array, and then in a loop to add random Numbers to the array. In the process of adding Numbers, we first look up whether there is this number in the array. If there is no such number, we will directly add it to the array. If this number exists, do not add. That's the way we think about it, that's the way we think about it, that's the way we think about it, and as I said, it's just a matter of efficiency.

To get a better idea of what this means, let's look at the details first: generate a random array of Numbers from 1 to 100, but the Numbers in the array cannot be repeated, that is, the positions are random, but the array elements cannot be repeated. In this case, instead of giving us the length of the array, we can make it any length between 1 and 100.

Let's take a look at how to implement it better, usually using an ArrayList, as shown in the following code:


package cn.sunzn.randomnumber;
import java.util.ArrayList;
import java.util.Random;
public class Demo {
   public static void main(String[] args) {
       Object[] values = new Object[20];
       Random random = new Random();
       ArrayList<Integer> list = new ArrayList<Integer>();
       for (int i = 0; i < values.length; i++) {
           int number = random.nextInt(100) + 1;
           if (!list.contains(number)) {
               list.add(number);
           }
       }
       values = list.toArray();
       
       for (int i = 0; i < values.length; i++) {
           System.out.print(values[i] + "t");
           if ((i + 1) % 10 == 0) {
               System.out.println("n");
           }
       }
   }
}

The above implementation process is relatively inefficient. Because every time you add it, you have to go through the list to see if it's in the current list, so it's order N^2. Let's think about it this way: since no repetition is involved, let's think about the functionality of hashsets and hashmaps. HashSet implements Set interface. Set is mathematically defined as a Set without repetition and without order. And the HashMap implementation Map, is also not allowed to repeat the Key. So we can do that using a HashMap or a HashSet.

When using the HashMap implementation, just need to convert its key into an array, the code is as follows:


package cn.sunzn.randomnumber;
import java.util.HashMap;
import java.util.Random;
public class Demo {
   public static void main(String[] args) {
       Object[] values = new Object[20];
       Random random = new Random();
       HashMap<Object, Object> hashMap = new HashMap<Object, Object>();
       
       for (int i = 0; i < values.length; i++) {
           int number = random.nextInt(100) + 1;
           hashMap.put(number, i);
       }
       
       values = hashMap.keySet().toArray();
       
       for (int i = 0; i < values.length; i++) {
           System.out.print(values[i] + "t");
           if ((i + 1) % 10 == 0) {
               System.out.println("n");
           }
       }
   }
}

Because the relation between HashSet and HashMap is too close, HashSet is implemented with HashMap at the bottom level, but there is no set of Value, only a set of Key, so it can also be implemented with HashSet, the code is as follows:


package cn.sunzn.randomnumber;
import java.util.HashSet;
import java.util.Random;
public class Demo {
   public static void main(String[] args) {
       Random random = new Random();
       Object[] values = new Object[20];
       HashSet<Integer> hashSet = new HashSet<Integer>();
       
       for (int i = 0; i < values.length; i++) {
           int number = random.nextInt(100) + 1;
           hashSet.add(number);
       }
       values = hashSet.toArray();
       
       for (int i = 0; i < values.length; i++) {
           System.out.print(values[i] + "t");
           if ((i + 1) % 10 == 0) {
               System.out.println("n");
           }
       }
   }
}

It's a little bit more efficient. So if we're going to limit the length of the array, we're just going to change the for loop and set it to a whlie loop. As follows:


package cn.sunzn.randomnumber;
import java.util.HashSet;
import java.util.Random;
public class Demo {
   public static void main(String[] args) {
       Random random = new Random();
       Object[] values = new Object[20];
       HashSet<Integer> hashSet = new HashSet<Integer>();
       
       while (hashSet.size() < values.length) {
           hashSet.add(random.nextInt(100) + 1);
       }
       values = hashSet.toArray();
       
       for (int i = 0; i < values.length; i++) {
           System.out.print(values[i] + "t");
           if ((i + 1) % 10 == 0) {
               System.out.println("n");
           }
       }
   }
}


Related articles: