指定区間内でK個の乱数を生成
4361 ワード
指定区間[min,max]でK個の乱数を生成する:
public class TestDemo8 {
private final static int MIN = 1;
private final static int MAX = 100;
private final static int GENERATE_NUMBER = 100;
public static void main(String[] args) throws Exception {
new TestDemo8().work(MIN, MAX, GENERATE_NUMBER);
System.out.println("----------- ---------------");
new TestDemo8().work2(MIN, MAX, GENERATE_NUMBER);
System.out.println("----------- ---------------");
new TestDemo8().work3(MIN, MAX, GENERATE_NUMBER);
}
/**
* @Description: ,
* @param min
* @param max
* @param k
* @throws Exception
*/
public void work(int min, int max, int k) throws Exception {
AssertUtil.assertExpress(max > min, "max must more than min");
AssertUtil.assertExpress(k >= 1, "generete random number must more than 1");
AssertUtil.assertExpress(k <= (max - min + 1), "can't generate more than " + (max - min + 1) + " number");
long startTime = System.currentTimeMillis();
int[] result = new int[k];
int[] seqNum = new int[max - min + 1];
for (int i = 0, size = seqNum.length; i < size; i++) {
seqNum[i] = i + min;
}
int end = max;
for (int i = 0; i < k; i++) {
int randomNum = RandomUtil.getRandomDigit(min, end);
result[i] = seqNum[randomNum - min];
seqNum[randomNum - min] = seqNum[end - min];
--end;
}
print(result);
System.out.println("case 1 --- length: " + result.length);
long endTime = System.currentTimeMillis();
System.out.println("case 1 --- spend time: " + (endTime - startTime));
}
/**
* @Description: , , ,
* @param min
* @param max
* @param k
* @throws Exception
*/
public void work2(int min, int max, int k) throws Exception {
AssertUtil.assertExpress(max > min, "max must more than min");
AssertUtil.assertExpress(k >= 1, "generete random number must more than 1");
AssertUtil.assertExpress(k <= (max - min + 1), "can't generate more than " + (max - min + 1) + " number");
long startTime = System.currentTimeMillis();
int[] arr = new int[max - min + 1];
for (int i = 0, size = arr.length; i < size; i++) {
arr[i] = i + min;
}
for (int i = 0; i < k; i++) {
int randomNum = RandomUtil.getRandomDigit(i, max - min);
swapArr(arr, i, randomNum);
}
print(arr);
System.out.println("case 2 --- length: " + k);
long endTime = System.currentTimeMillis();
System.out.println("case 2 --- spend time: " + (endTime - startTime));
}
private void swapArr(int[] arr, int i, int j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
private void swapList(List<Integer> list, int i, int j) {
list.set(i, list.set(j, list.get(i)));
}
/**
* @Description: , ,max k , ,
* @param min
* @param max
* @param k
* @throws Exception
*/
public void work3(int min, int max, int k) throws Exception {
AssertUtil.assertExpress(max > min, "max must more than min");
AssertUtil.assertExpress(k >= 1, "generete random number must more than 1");
AssertUtil.assertExpress(k <= (max - min + 1), "can't generate more than " + (max - min + 1) + " number");
long startTime = System.currentTimeMillis();
List<Integer> result2 = new ArrayList<Integer>(k);
while (k > 0) {
int step = (max - min) / k;
if (step < 1) {
for (int i = min; i <= max; i++) {
result2.add(i);
}
break;
}
int randomNum = RandomUtil.getRandomDigit(1, step);
min += randomNum;
result2.add(min);
k--;
}
for (int i = result2.size(); i >= 1; i--) {
swapList(result2, i - 1, RandomUtil.getRandomDigit(0, i - 1));
}
print(result2);
System.out.println("case 3 --- length: " + result2.size());
long endTime = System.currentTimeMillis();
System.out.println("case 3 --- spend time: " + (endTime - startTime));
}
private void print(int[] arr) {
for (int i = 0, length = arr.length; i < length; i++) {
System.out.print(arr[i] + " ");
}
}
private void print(List<Integer> list) {
for (Integer value : list) {
System.out.print(value + " ");
}
}
}
public class TestDemo8 {
private final static int MIN = 1;
private final static int MAX = 100;
private final static int GENERATE_NUMBER = 100;
public static void main(String[] args) throws Exception {
new TestDemo8().work(MIN, MAX, GENERATE_NUMBER);
System.out.println("----------- ---------------");
new TestDemo8().work2(MIN, MAX, GENERATE_NUMBER);
System.out.println("----------- ---------------");
new TestDemo8().work3(MIN, MAX, GENERATE_NUMBER);
}
/**
* @Description: ,
* @param min
* @param max
* @param k
* @throws Exception
*/
public void work(int min, int max, int k) throws Exception {
AssertUtil.assertExpress(max > min, "max must more than min");
AssertUtil.assertExpress(k >= 1, "generete random number must more than 1");
AssertUtil.assertExpress(k <= (max - min + 1), "can't generate more than " + (max - min + 1) + " number");
long startTime = System.currentTimeMillis();
int[] result = new int[k];
int[] seqNum = new int[max - min + 1];
for (int i = 0, size = seqNum.length; i < size; i++) {
seqNum[i] = i + min;
}
int end = max;
for (int i = 0; i < k; i++) {
int randomNum = RandomUtil.getRandomDigit(min, end);
result[i] = seqNum[randomNum - min];
seqNum[randomNum - min] = seqNum[end - min];
--end;
}
print(result);
System.out.println("case 1 --- length: " + result.length);
long endTime = System.currentTimeMillis();
System.out.println("case 1 --- spend time: " + (endTime - startTime));
}
/**
* @Description: , , ,
* @param min
* @param max
* @param k
* @throws Exception
*/
public void work2(int min, int max, int k) throws Exception {
AssertUtil.assertExpress(max > min, "max must more than min");
AssertUtil.assertExpress(k >= 1, "generete random number must more than 1");
AssertUtil.assertExpress(k <= (max - min + 1), "can't generate more than " + (max - min + 1) + " number");
long startTime = System.currentTimeMillis();
int[] arr = new int[max - min + 1];
for (int i = 0, size = arr.length; i < size; i++) {
arr[i] = i + min;
}
for (int i = 0; i < k; i++) {
int randomNum = RandomUtil.getRandomDigit(i, max - min);
swapArr(arr, i, randomNum);
}
print(arr);
System.out.println("case 2 --- length: " + k);
long endTime = System.currentTimeMillis();
System.out.println("case 2 --- spend time: " + (endTime - startTime));
}
private void swapArr(int[] arr, int i, int j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
private void swapList(List<Integer> list, int i, int j) {
list.set(i, list.set(j, list.get(i)));
}
/**
* @Description: , ,max k , ,
* @param min
* @param max
* @param k
* @throws Exception
*/
public void work3(int min, int max, int k) throws Exception {
AssertUtil.assertExpress(max > min, "max must more than min");
AssertUtil.assertExpress(k >= 1, "generete random number must more than 1");
AssertUtil.assertExpress(k <= (max - min + 1), "can't generate more than " + (max - min + 1) + " number");
long startTime = System.currentTimeMillis();
List<Integer> result2 = new ArrayList<Integer>(k);
while (k > 0) {
int step = (max - min) / k;
if (step < 1) {
for (int i = min; i <= max; i++) {
result2.add(i);
}
break;
}
int randomNum = RandomUtil.getRandomDigit(1, step);
min += randomNum;
result2.add(min);
k--;
}
for (int i = result2.size(); i >= 1; i--) {
swapList(result2, i - 1, RandomUtil.getRandomDigit(0, i - 1));
}
print(result2);
System.out.println("case 3 --- length: " + result2.size());
long endTime = System.currentTimeMillis();
System.out.println("case 3 --- spend time: " + (endTime - startTime));
}
private void print(int[] arr) {
for (int i = 0, length = arr.length; i < length; i++) {
System.out.print(arr[i] + " ");
}
}
private void print(List<Integer> list) {
for (Integer value : list) {
System.out.print(value + " ");
}
}
}