[Java] The shuffle method in the Collections classes is used to improve the generation of non-repeated random numbers within a certain range. collectionsshuffle

Source: Internet
Author: User

[Java] The shuffle method in the Collections classes is used to improve the generation of non-repeated random numbers within a certain range. collectionsshuffle

The method mentioned in the last time "Java generates different random numbers within a certain range" (click to open the link), although the problem of generating different random numbers within a certain range has been solved, the running speed is good, at least not long enough. In fact, using the shuffle method in the Collections class can produce different random numbers in a certain range with a clearer speed and faster speed.

The shuffle method under the Collections class is a program that can randomly disrupt the elements in an array, also called the shuffle method.

This method works with the dynamic array ArrayList introduced in [Java] Collections class in Java -- upgraded data structure in Java (click to open the link, this question has ended:

Import java. util. *; public class shuffleTest {public static void main (String [] args) {// create a dynamic array ArrayList <Integer> randomArr = new ArrayList <Integer> (); // insert 1-20 in it. You can modify this for loop and add for (int I = 1; I <= 20; I ++) {randomArr. add (I);} // shuffles the dynamic array Collections. shuffle (randomArr); // print this dynamic array System. out. println (randomArr );}}

In this way, non-repeated random numbers are produced within the range of 1-20. The running result is as follows:


On the basis of this, let the program generate 300 sets of random numbers in the range of 1-N, and do not repeat them. To test the performance of this program, see the following code:

Import java. util. *; public class shuffleTest {public static void main (String [] args) {ArrayList <Integer> randomArr = new ArrayList <Integer> (); for (int n = 1; n <= 300; n ++) {System. out. printf ("random number generated in the range of 1-% d, not repeated", n); for (int I = 1; I <= n; I ++) {randomArr. add (I);} Collections. shuffle (randomArr); System. out. println (randomArr); randomArr = new ArrayList <Integer> ();}}}

The program running result is as follows. The running speed is much faster than the method mentioned in [Java] generating different random numbers within a certain range "(click to open the link.


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.