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]+ ""); }
}
}