forked from leavesCZY/AndroidGuide
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRandomUtil.java
43 lines (34 loc) · 1.2 KB
/
RandomUtil.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package utils;
import java.util.Random;
public class RandomUtil {
public static void main(String[] args) {
}
//返回 min 到 max 之间的 int,包括 min 和 max
private static int fun1(int min, int max) {
if (min >= max) {
throw new IllegalArgumentException();
}
return min + new Random().nextInt(max - min + 1);
}
//返回 min 到 max 之间的 long,包括 min 和 max
private static long fun2(long min, long max) {
if (min >= max) {
throw new IllegalArgumentException();
}
return min + (long) (new Random().nextDouble() * (max - min + 1));
}
//返回 min 到 max 之间的 double,包括 min ,不包括 max
private static double fun3(double min, double max) {
if (min >= max) {
throw new IllegalArgumentException();
}
return min + new Random().nextDouble() * (max - min);
}
//返回 min 到 max 之间的 float,包括 min ,不包括 max
private static double fun4(float min, float max) {
if (min >= max) {
throw new IllegalArgumentException();
}
return min + new Random().nextFloat() * (max - min);
}
}