The generation of complete random numbers in Java

Source: Internet
Author: User

Qingming season, outside the rain, listening to the patter of rain brush, my mood is gradually calmed down, such as weak blue sky but there are clouds floating on the above, nonsense not to say, the following into the topic:

This time to share with you how to produce a completely different random array, you may all know how to produce a random array, but the problem is that the random array we produce is likely to have many of the same numbers, and sometimes we need to produce a series of completely different numbers, with the following two ways:

Method One: Comparative method

Compare each array of arrays over and over again, if the same one continues to produce new numbers and then compares them until each number is different, as follows:

 import Java.util.Random;
    
       Publicclass Ran {publicstaticvoid main (String args[]) {Ran r=new Ran ();
    
       R.initarr ();
    
       R.ranarr ();
    
    R.print ();
    
    } INTARR[]=NEWINT[10];
    
       Initializes an array of publicvoid Initarr () {Random ran=new Random ();
    
       for (int i=0;i<arr.length;i++) {arr[i]=ran.nextint (10);
    
       }///Make the number inside the array completely random publicvoid Ranarr () {int n=1;
    
       Boolean isnew=false;
    
       Random r=new Random (); while (!isnew) {for (int k=0;k<n;k++) {for (int i=0;i<arr.length;i++) {f or (int j=i+1;j<arr.length;j++) {if (Arr[i]!=arr[j]) {Isnew=tru
    
                     E
    
                         } else{Arr[i]=r.nextint (10);
    
      n++;               }}}} System.out.prin
    
    TLN ("cyclic" +n+ ""); Output full random number array publicvoid print () {for (int i=0;i<arr.length;i++) {SYSTEM.O
    
       Ut.print (arr[i]+ ""); }
    
    }
    
}

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.