org.apache.commons.lang3.RandomStringUtils&RandomUtils

概述

随机数工具包提供了一系列方便获取随机数的方法

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

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值