forked from apex-commons/SoqlBuilder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RandomUtils.cls
101 lines (83 loc) · 2.9 KB
/
RandomUtils.cls
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
global class RandomUtils {
private static final IntegerRange BINARY_RANGE = new IntegerRange(0,1);
global static Boolean nextBoolean(){
return nextInteger(BINARY_RANGE,true) == 1;
}
global static Integer nextInteger(){
return nextInteger(ApexLangUtils.MAX_INTEGER_RANGE,true);
}
global static Integer nextInteger(Integer max){
return nextInteger(new IntegerRange(0,Math.abs(max)),true);
}
global static Integer nextInteger(Integer max, Boolean inclusive){
return nextInteger(new IntegerRange(0,Math.abs(max)),inclusive);
}
global static Integer nextInteger(Integer min, Integer max){
return nextInteger(new IntegerRange(min,max),true);
}
global static Integer nextInteger(Integer min, Integer max, Boolean inclusive){
return nextInteger(new IntegerRange(min,max),inclusive);
}
global static Integer nextInteger(IntegerRange range){
return nextInteger(range,true);
}
global static Integer nextInteger(IntegerRange range, Boolean inclusive){
if(range == null){
return null;
}
if(inclusive && (range.max() - range.min()) <= 0){
return range.min();
}
if(!inclusive && (range.max() - range.min()) <= 2){
return range.min();
}
return (Integer) (
Math.round(
Math.floor(
Math.random()
* ((range.max()-range.min()+(inclusive ? 1 : -1)))
)
)
+ range.min()
+ (inclusive ? 0 : 1)
);
}
global static Long nextLong(){
return nextLong(ApexLangUtils.MAX_LONG_RANGE,true);
}
global static Long nextLong(Long max){
return nextLong(new LongRange(0,Math.abs(max)),true);
}
global static Long nextLong(Long max, Boolean inclusive){
return nextLong(new LongRange(0,Math.abs(max)),inclusive);
}
global static Long nextLong(Long min, Long max){
return nextLong(new LongRange(min,max),true);
}
global static Long nextLong(Long min, Long max, Boolean inclusive){
return nextLong(new LongRange(min,max),inclusive);
}
global static Long nextLong(LongRange range){
return nextLong(range,true);
}
global static Long nextLong(LongRange range, Boolean inclusive){
if(range == null){
return null;
}
if(inclusive && (range.max() - range.min()) <= 0){
return range.min();
}
if(!inclusive && (range.max() - range.min()) <= 2){
return range.min();
}
return
Math.round(
Math.floor(
Math.random()
* ((range.max()-range.min()+(inclusive ? 1 : -1)))
)
)
+ range.min()
+ (inclusive ? 0 : 1);
}
}