Merge pull request #764 from init-qy/v5-dev

add method for RandomUtil
This commit is contained in:
Golden Looly 2020-03-08 16:53:44 +08:00 committed by GitHub
commit 3c1060769e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -374,7 +374,40 @@ public class RandomUtil {
return result;
}
/**
* 创建指定长度的随机索引
*
* @param length 长度
* @return 随机索引
*/
public static int[] createRandomList(int length){
int[] list = ArrayUtil.range(length);
for (int i = 0; i < length; i++) {
int random = randomInt(i,length);
ArrayUtil.swap(list,i,random);
}
return list;
}
/**
* 随机获得列表中的一定量的元素返回List
*
* @param source 列表
* @param count 随机取出的个数
* @param <T> 元素类型
* @return 随机列表
*/
public static <T> List<T> randomEleList(List<T> source, int count){
if(count >= source.size()){
return source;
}
int[] randomList = ArrayUtil.sub(createRandomList(source.size()),0,count);
List<T> result = new ArrayList<>();
for (int e: randomList){
result.add(source.get(e));
}
return result;
}
/**
* 获得一个随机的字符串只包含数字和字符
*