获取随机数并解决重复问题
package chapter5;
import java.util.Random;
public class RandomDecimal {
public static void main(String[] args) {
RandomDecimal rdc=new RandomDecimal();
rdc.getNum(10);
}
public int[] getNum(int length){
int[] nums=new int[length];
Random rd=new Random();
for (int i = 0; i < nums.length; i++) {
nums[i]=rd.nextInt(33);
for (int j = 0; j <i; j++) {
if(nums[i]==nums[j]){
i--;
break;
}
}
}
for (int i = 0; i < nums.length; i++) {
System.out.print(nums[i]+"\t");
}
System.out.println();
return nums;
}
}