Skip to content

Commit a917ecf

Browse files
committed
Merge pull request functionaljava#112 from xuwei-k/Rand-standard-overflow
avoid overflow
2 parents 6659ce3 + c71f0d4 commit a917ecf

1 file changed

Lines changed: 8 additions & 3 deletions

File tree

core/src/main/java/fj/test/Rand.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,14 @@ public F<Integer, F<Integer, Integer>> f(final Option<Long> seed) {
130130
public F<Integer, Integer> f(final Integer from) {
131131
return new F<Integer, Integer>() {
132132
public Integer f(final Integer to) {
133-
final int f = min(from, to);
134-
final int t = max(from, to);
135-
return f + seed.map(fr).orSome(new Random()).nextInt(t - f + 1);
133+
if(from == to){
134+
return from;
135+
}else{
136+
final int f = min(from, to);
137+
final int t = max(from, to);
138+
final int x = Math.abs(t - f);
139+
return f + seed.map(fr).orSome(new Random()).nextInt(x == Integer.MIN_VALUE ? Integer.MAX_VALUE : x);
140+
}
136141
}
137142
};
138143
}

0 commit comments

Comments
 (0)