On the performance problem of obtaining a set of non-repeating random numbers in Java

Source: Internet
Author: User

Today in a Java project, using a set of non-repeating random number problems, no matter how random number inside there are always a few are repeated.

So the Internet to find information, found on the internet most of the information is a train of thought:

Online Thinking One: (performance is not ideal)

A random number is generated, and then matches the previous random number when generating the next random number, and if there is a random number currently generated, it is regenerated until there is no current generated number in the random array that was generated. Although this can be achieved by generating a set of non-repeating random numbers, but the performance is not very good, if we need to generate 10 numbers, in the generation of the first, do not match, in the second generation and the first match, because it is a random number, then he has a repetition probability of 1/10, Generate the third time and the first second to match, his chance of repetition is 2/10, when the last time to generate the final match with the first 9, then the last generation of the number has a 9/10 chance and the previous number repeats, because 10 9 of the probability is close to 1, that is, generate random numbers The more likely you are to repeat in the next 1, the more times you need to regenerate, the worse the performance will be.

I analyze the idea of two:

My idea is that only generate a random number as a template, there is a repetition, in the process of generating this set of numbers in the order of a set, and then the set of random number and the number of the set to match, the collection of those existing in the random number to remove, then the collection is left in the random array of numbers, Finally, the last number in the random array is replaced by the first number in the collection, then the first of the set is removed, and so on until the number in the collection is exhausted.

     Public Static voidMain (string[] args) {//TODO auto-generated Method Stub//On- line method to get non-repeating random numberTestA (10000); //Custom methods get non-repeating random numbersTESTB (10000);    }    //Custom methods get non-repeating random numbers    Private Static voidTESTB (intSZ) {        LongStarttime=system.currenttimemillis ();//Start test TimeRandom rd =NewRandom (); int[] rds =New int[SZ];//Random number Array        intn = 0;//Serial Numberlist<integer> LST =NewArraylist<integer> ();//storing an ordered set of numbers//get an array of random numbers with duplicate numbers inside         while(N <rds.length) {lst.add (n); Rds[n++] = (int) (Rd.nextfloat () *sz); }        //The random number and the ordered set are pairs, and the number of random numbers in the set is removed from the set.         for(inti = 0; i < rds.length; i++) {             for(intj = 0; J < Lst.size (); J + +) {                if(Rds[i] = =Lst.get (j))                    {Lst.remove (j);  Break; }            }        }        //replaces the second number repeated in the array with the first digit of the collection and removes the first digit of the array         for(inti = 0; i < rds.length; i++) {             for(intj = 0; J < Rds.length; J + +) {                if(I! = J && rds[i] = =Rds[j]) {Rds[j]= Lst.get (0); Lst.remove (0);  Break; }            }        }        //the resulting RDS array is a non-repeating random array        LongEndtime=system.currenttimemillis ();//Get end TimeSYSTEM.OUT.PRINTLN ("Custom Code Run Time:" + (Endtime-starttime) + "MS"); }    //On- line method to get non-repeating random number    Private Static voidTestA (intSZ) {        LongStarttime=system.currenttimemillis ();//Start test TimeRandom random =NewRandom (); intA[] =New int[SZ];  for(inti = 0; i < a.length; i++) {A[i]=Random.nextint (SZ);  for(intj = 1; J < I; J + +) {                 while(A[i] = = A[j]) {//if repeat, return to regenerate random numberi--; }            }        }        LongEndtime=system.currenttimemillis ();//Get end TimeSystem.out.println ("Online Idea code run time:" + (Endtime-starttime) + "MS"); }

The larger the test number, the more effective it will be.

The above has insufficient place also invites everybody to forgive!

On the performance problem of obtaining a set of non-repeating random numbers in Java

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.