All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.knightliao.apollo.utils.common.RandomUtil Maven / Gradle / Ivy

package com.github.knightliao.apollo.utils.common;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;

/**
 * 
 * @author liaoqiqi
 * @version 2014-1-15
 */
public final class RandomUtil {

    private RandomUtil() {

    }

    /**
     * 获取一堆数据
     * 
     * @param limit
     * @return
     */
    public static List randomSerial(int limit) {
        List list = new ArrayList(limit);

        for (int ix = 0; ix < limit; ++ix) {
            list.add(ix);
        }

        Collections.shuffle(list, new Random());
        return list;
    }

    public static void main(String[] args) {
        System.out.println((int) 1 / 20 + "," + random(0, 0) + ","
                + random(3, 2) + "," + random(-2, 4));
        List a = RandomUtil.randomSerial(10);
        for (int i : a) {
            System.out.println(i);
        }
        // for(int i = 0 ;i < 500 ; i++){
        // System.out.println(random(0, 20));
        // }
    }

    /**
     * [min,max]
     * 
     * @param min
     * @param max
     * @return
     */
    public static int random(int min, int max) {
        if (min < 0 || max < 0) {
            throw new RuntimeException(
                    "illegal argment, min and max must great then zero.");
        }
        if (min > max) {
            int t = max;
            max = min;
            min = t;
        } else if (min == max) {
            return min;
        }

        Random random = new Random();
        return random.nextInt(max) % (max - min + 1) + min;

    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy