"Java" uses the shuffle shuffling method under the collections class to improve the generation of non-repeating random numbers within a certain range

Source: Internet
Author: User
Tags shuffle

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.