package com.gh.common.util; import java.math.BigDecimal; 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 { index[i] = 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); } /** * 四舍五入取整 * @return */ public static int getInt(double d) { BigDecimal bigDecimal = new BigDecimal(d).setScale(0, BigDecimal.ROUND_HALF_UP); return bigDecimal.intValue(); } }