com.silentgo.utils.random.RandomUtil Maven / Gradle / Ivy
package com.silentgo.utils.random;
import java.util.Random;
/**
* Project : silentgo
* com.silentgo.utils.random
*
* @author teddyzhu
*
* Created by teddyzhu on 16/9/19.
*/
public class RandomUtil {
private static final Random random = new Random();
private static final String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
private static final Integer len = str.length();
/**
* 生成验证码
*
* @return
*/
public static char getAuthCodeChar() {
return str.charAt(number(0, len));
}
public static String String(int length) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < length; i++) {
int num = random.nextInt(62);
sb.append(str.charAt(num));
}
return sb.toString();
}
/**
* 产生两个数之间的随机数
*
* @param min 小数
* @param max 比min大的数
* @return int 随机数字
*/
public static int number(int min, int max) {
return min + random.nextInt(max - min);
}
/**
* 产生0--number的随机数,不包括num
*
* @param number 数字
* @return int 随机数字
*/
public static int number(int number) {
return random.nextInt(number);
}
/**
* 生成RGB随机数
*
* @return
*/
public static int[] getRandomRgb() {
int[] rgb = new int[3];
for (int i = 0; i < 3; i++) {
rgb[i] = random.nextInt(255);
}
return rgb;
}
}