public int[] getRandomFromArray(int[] array, int count) {
// ArrayList<Integer>arrayList =null;
int[] a = array;
int[] result = new int[count];
boolean r[] = new boolean[array.length];
Random random = new Random();
int m = count; // 要随机取的元素个数
if (m > a.length || m < 0)
return a;
int n = 0;
while (true) {
int temp = random.nextInt(a.length);
if (!r[temp]) {
if (n == m) // 取到足量随机数后退出循环
break;
n++;
System.out.println("得到的第" + n + "个随机数为:" + a[temp]);
result[n - 1] = a[temp];
r[temp] = true;
}
}
return result;
}
转载于:https://blog.51cto.com/shamrock/1337647