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

com.github.shoothzj.javatool.util.RandomUtil Maven / Gradle / Ivy

There is a newer version: 3.1.15
Show newest version
package com.github.shoothzj.javatool.util;

import java.security.SecureRandom;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;

public class RandomUtil {

    private static final SecureRandom secureRandom = new SecureRandom();

    public static boolean randomBoolean() {
        return ThreadLocalRandom.current().nextInt(1) == 0;
    }

    public static int nextInt() {
        return ThreadLocalRandom.current().nextInt();
    }

    public static int nextInt(int bound) {
        return ThreadLocalRandom.current().nextInt(bound);
    }

    public static int randomInt(int min, int max) {
        return ThreadLocalRandom.current().nextInt(max - min) + min;
    }

    public static > T randomEnum(Class clazz){
        int x = ThreadLocalRandom.current().nextInt(clazz.getEnumConstants().length);
        return clazz.getEnumConstants()[x];
    }

    public static E chooseOne(List list) {
        return list.get(nextInt(list.size()));
    }

    public byte[] getSecRanByte(int len) {
        byte[] bytes = new byte[len];
        secureRandom.nextBytes(bytes);
        return bytes;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy