概述
随机数工具包提供了一系列方便获取随机数的方法
RandomStringUtils
static String randomNumeric(int count)
获取随机数字
// 获取随机3位数:697
System.out.println(RandomStringUtils.randomNumeric(3));
// 获取随机3-5位数
System.out.println(RandomStringUtils.randomNumeric(3,6));
static String randomAlphabetic(int count)
获取随机字母
// 从:a-z,A-Z 中随机获取4位字母,例如:uwJG
System.out.println(RandomStringUtils.randomAlphabetic(4));
// 从:a-z,A-Z 中随机获取2-3位字母,例如:rR
System.out.println(RandomStringUtils.randomAlphabetic(2,4));
static String randomAlphanumeric(int count)
获取随机数字+字母
// 从:a-z,A-Z,0-9 中随机获取4位数字+字母,例如:0ZCC
System.out.println(RandomStringUtils.randomAlphanumeric(4));
// 从:a-z,A-Z,0-9 中随机获取2-3位数字+字母,例如:5i
System.out.println(RandomStringUtils.randomAlphanumeric(2,4));
RandomUtils
这下面的方法比较见名知意
static boolean nextBoolean()
获取随机一个真假
static byte[] nextBytes(int count)
随机的字节数组,1字节:[-128,127]
// [86, -103]
System.out.println(Arrays.toString(RandomUtils.nextBytes(2)));
static double nextDouble()
static double nextDouble(double startInclusive, double endInclusive)
获取随机double
static float nextFloat()
static float nextFloat(float startInclusive, float endInclusive)
获取随机的float
static int nextInt()
static int nextInt(int startInclusive, int endExclusive)
获取随机的int
static long nextLong()
static long nextLong(long startInclusive, long endExclusive)
获取随机的long