java commons lang 随机数_Java Language - Generating Random number using apache-common lang3 | java Tuto...

Example

We can use org.apache.commons.lang3.RandomUtils to generate random numbers using a single line.

int x = RandomUtils.nextInt(1, 1000);

The method nextInt(int startInclusive, int endExclusive) takes a range.

Apart from int, we can generate random long, double, float and bytes using this class.

RandomUtils class contains the following methods-

static byte[] nextBytes(int count) //Creates an array of random bytes.

static double nextDouble() //Returns a random double within 0 - Double.MAX_VALUE

static double nextDouble(double startInclusive, double endInclusive) //Returns a random double within the specified range.

static float nextFloat() //Returns a random float within 0 - Float.MAX_VALUE

static float nextFloat(float startInclusive, float endInclusive) //Returns a random float within the specified range.

static int nextInt() //Returns a random int within 0 - Integer.MAX_VALUE

static int nextInt(int startInclusive, int endExclusive) //Returns a random integer within the specified range.

static long nextLong() //Returns a random long within 0 - Long.MAX_VALUE

static long nextLong(long startInclusive, long endExclusive) //Returns a random long within the specified range.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值