学习apache commons lang3的源代码 (1):前言和R

本系列主要是针对lang3的3.7版本的源代码进行学习,并适当举例。一共大概150多个java文件,争取30天内学习完毕。

26个英文字母 争取每天学习1个字母开头的类们。

 

今天,就学习R开头的吧。

 

第一个:RandomUtils。

这个类,是对Random类 (java.util) 的补充。

这个类,的代码不算复杂。因为是Random的helper。因此,在其内部,首先声明一个:

 private static final Random RANDOM = new Random();

 

1)有些方法,实际上就是直接调用Random的方法。比如:nextBoolean();

public static boolean nextBoolean() {
return RANDOM.nextBoolean();
}

2)有些方法,实际上就是做一些校验,然后,增强型的直接调用Random的方法。比如:nextBytes。

public static byte[] nextBytes(final int count) {
Validate.isTrue(count >= 0, "Count cannot be negative.");

final byte[] result = new byte[count];
RANDOM.nextBytes(result);
return result;
}

3)有些方法,是Random没有直接提供的。比如:nextInt,是返回两个整数之间的随机值。

public static int nextInt(final int startInclusive, final int endExclusive) {
Validate.isTrue(endExclusive >= startInclusive,
"Start value must be smaller or equal to end value.");
Validate.isTrue(startInclusive >= 0, "Both range values must be non-negative.");

if (startInclusive == endExclusive) {
return startInclusive;
}

return startInclusive + RANDOM.nextInt(endExclusive - startInclusive);
}

至于nextLong,则和nextInt类似。

4)

转载于:https://www.cnblogs.com/aomi/p/7909581.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值