Import java.util.ArrayList; import java.util.random; public class test {
Public static void main (string [] args) {test test = new test (); for (int n = 2; n <20; n ) {int [] r = test.myrand (n); system.out.println (N ":" Test.ArrayTString (r, ","));}} public int [] myrand (int N) {int [] result = new int [n]; arraylist al = new arraylist (); For (int i = 1; i <= n; i ) {al.add (new integer (i));} // for (INT i = 0; i /// The following is the idea provided by netizens // Providing a random method of Java to generate a number, then MOD 12, if there is the same value as the previous value, then discard this value, otherwise save until 12 in the array. There is also a method to implement this: Define an array with a length of the array of inputs, and initialize the integer, that is, when its length is initialized, A [0] = 1, a [1] = 2, a [2 ] = 3, ..., a [11] = 12. The generated array is B [12], generates an integer of 1 to 12 using a random function, as a subscript of the A array, then assigning the value to the B array, assigning a value to 0, each time assignment B before checking this A is 0, it is not assigned, it is another A. This should be better than the previous algorithm. Another one is to reduce a value, such as the first MOD 12, the second MOD 11 .............. If MOD is given each time MOD, you must get a value every time, each time you have to have the original array. To rearrange, this ensures that a random number can be obtained each time, and there is no time to waste too much. This method is an improvement in the second algorithm described above.