Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - The method of randomly generating different T integers from 1 to n in java
The method of randomly generating different T integers from 1 to n in java
public int[] fn(int n,int t)

{

ArrayList & ltint & gtnumbers = new ArrayList();

int[]rt numbers = new int[t];

for(int I = 0; I & ltn;; I++){ // Initialize the array

numbers . add(I+ 1);

}

for(int j = 0; j & ltt; j++){

int raNum =(int)(math . random()* numbers . size());

rt numbers[j]= numbers . get(raNum);

numbers . remove(raNum);

}

Return to rtnumbers

}

The idea is to put the number in a list first, then randomly generate the subscript raNum, take the number out of the list and put it in rtnumbers, and then delete the number from the list to achieve the purpose of not repeating it. What's upstairs is completely wrong and unintelligible.