The last time in the "Java" in a certain range of different random number (click Open link) mentioned on the method, although the problem has been solved a certain range of different random numbers, the speed of operation is already possible, at least not for a long time to get bad, In fact, using the idea of shuffle method under the collections class can produce different random numbers in a certain range more clearly and faster.
The shuffle method under the collections class is a program that can randomly disrupt an element in an array, also called a shuffle method.
With this approach, with the dynamic array ArrayList described in the "Java" Java Collections Class--java (click the Open link), this question has been terminated:
Import java.util.*;p Ublic class Shuffletest {public static void main (string[] args) {//Create a dynamic array arraylist<integer> Randomarr=new arraylist<integer> ();//In the inside of the 1-20, you like to completely change the for loop, in the inside plug anything can be for (int i=1;i<=20;i++) { Randomarr.add (i);} Shuffle Collections.shuffle (Randomarr) for this dynamic array;//print this dynamic array System.out.println (Randomarr);}}
This enables the production of non-repeating random numbers in the range of 1-20, with the following results:
The author again on this basis to let the program in the 1-n range of 300 random numbers, not repeat, to test the performance of this program, see the following code:
Import java.util.*;p Ublic class Shuffletest {public static void main (string[] args) {arraylist<integer> randomarr= New Arraylist<integer> (); for (int n=1;n<=300;n++) {System.out.printf ("generates a random number in the range of 1-%d, does not repeat", n); for (int i=1;i <=n;i++) {randomarr.add (i);} Collections.shuffle (Randomarr); System.out.println (Randomarr); randomarr=new arraylist<integer> ();}}
The results of the program run as follows, the speed of operation is significantly faster than the previous "Java" in a certain range of different random number (click on the link) mentioned on the method is much faster.
"Java" uses the shuffle shuffling method under the collections class to improve the generation of non-repeating random numbers within a certain range