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 {
                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);
    }

}
