The first method is to randomly generate a number in an array to be selected, put it at the end of the array to be selected, and then randomly generate the next random number from length-1, and so on.
Public static int [] randoms () { Random r = new random (); Int temp1, temp2; Int send [] = }; Int Len = Send. length; Int returnvalue [] = new int [22]; For (INT I = 0; I <22; I ++) { Temp1 = math. Abs (R. nextint () % Len; Returnvalue [I] = Send [temp1]; Temp2 = Send [temp1]; Send [temp1] = Send [len-1]; Send [len-1] = temp2; Len --; } Return returnvalue; } } |
Method 2: select a Fixed Array without duplicates and randomly change the positions of the array. After multiple times, the array is a random array without duplicates.
Public static int [] random2 () { Int send [] = }; Int temp1, temp2, temp3; Random r = new random (); For (INT I = 0; I <send. length; I ++) // random exchange send. length times { Temp1 = math. Abs (R. nextint () % (send. Length-1); // randomly generate a location Temp2 = math. Abs (R. nextint () % (send. Length-1); // randomly generate another location If (temp1! = Temp2) { Temp3 = Send [temp1]; Send [temp1] = Send [temp2]; Send [temp2] = temp3; } } Return send; } |