/**
* 随机指定范围内N个不重复的数
* 最简单最基本的方法
* @param min 指定范围最小值
* @param max 指定范围最大值
* @param n 随机数个数
*/
public static int[] randomCommon(int min, int max, int n){
if (n > (max - min + 1) || max < min) {
return null;
}
int[] result = new int[n];
int count = 0;
while(count < n) {
int num = (int) (Math.random() * (max - min)) + min;
boolean flag = true;
for (int j = 0; j < n; j++) {
if(num == result[j]){
flag = false;
break;
}
}
if(flag){
result[count] = num;
count++;
}
}
return result;
}
/**
* 将一个正整数L随机拆分成n个正整数
* @param L 正整数
* @param n 个数
*/
public static int[] random(int L, int n){
int[] result = new int[n];
Random rand = new Random();
int sum = 0; // 已生成的随机数总和
for(int i=0; i<n-1; i++){
int temp = L - sum;
int random = rand.nextInt(temp);
result[i] = random;
sum += random;
}
result[n-1] = L - sum;
return result;
}