完成阅读:推荐、原创、资讯、攻略界面UI
This commit is contained in:
44
app/src/main/java/com/gh/common/util/RandomUtils.java
Normal file
44
app/src/main/java/com/gh/common/util/RandomUtils.java
Normal file
@ -0,0 +1,44 @@
|
||||
package com.gh.common.util;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
/**
|
||||
* Created by LGT on 2016/6/7.
|
||||
*/
|
||||
public class RandomUtils {
|
||||
|
||||
/*
|
||||
* 从 size 范围内随机 count 个不重复的数字
|
||||
*/
|
||||
public static int[] getRandomArray(int count, int size) {
|
||||
int[] index = new int[count];
|
||||
Random random = new Random(System.currentTimeMillis());
|
||||
for (int i = 0; i < index.length; i++) {
|
||||
if (i == 0) {
|
||||
index[i] = random.nextInt(size);
|
||||
} else {
|
||||
random(random, index, i, size);
|
||||
}
|
||||
}
|
||||
return index;
|
||||
}
|
||||
|
||||
private static int random(Random random, int[] index, int i, int size) {
|
||||
int seed = random.nextInt(size);
|
||||
for (int j = 0; j < i; j++) {
|
||||
if (index[j] == seed) {
|
||||
return random(random, index, i, size);
|
||||
}
|
||||
}
|
||||
return seed;
|
||||
}
|
||||
|
||||
/*
|
||||
* 从 size 范围内随机一个数字
|
||||
*/
|
||||
public static int nextInt(int size) {
|
||||
Random random = new Random(System.currentTimeMillis());
|
||||
return random.nextInt(size);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user