Skip to content

Commit f552a20

Browse files
committed
Support 64-bit integers in rand()
1 parent 354cce4 commit f552a20

File tree

1 file changed

+7
-11
lines changed
  • src/main/java/com/laytonsmith/core/functions

1 file changed

+7
-11
lines changed

src/main/java/com/laytonsmith/core/functions/Math.java

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1115,9 +1115,9 @@ public Integer[] numArgs() {
11151115

11161116
@Override
11171117
public String docs() {
1118-
return "mixed {[] | min/max, [max]} Returns a random number from 0 to max, or min to max, depending on usage. Max is exclusive. Min must"
1119-
+ " be less than max, and both numbers must be >= 0. This will return an integer. Alternatively, you can pass no arguments, and a random"
1120-
+ " double, from 0 to 1 will be returned.";
1118+
return "mixed {[] | min/max, [max]} Returns a random number from 0 to max or min to max, depending on usage."
1119+
+ " Max is exclusive. Min must be less than max. This will return an integer."
1120+
+ " If no arguments are given, a random double from 0.0 to 1.0 (exclusive) will be returned.";
11211121
}
11221122

11231123
@Override
@@ -1148,17 +1148,13 @@ public Mixed exec(Target t, Environment env, Mixed... args) throws CancelCommand
11481148
min = ArgumentValidation.getInt(args[0], t);
11491149
max = ArgumentValidation.getInt(args[1], t);
11501150
}
1151-
if(max > Integer.MAX_VALUE || min > Integer.MAX_VALUE) {
1152-
throw new CRERangeException("max and min must be below int max, defined as " + Integer.MAX_VALUE,
1153-
t);
1151+
if(max <= min) {
1152+
throw new CRERangeException("max must be greater than min", t);
11541153
}
11551154

11561155
long range = max - min;
1157-
if(range <= 0) {
1158-
throw new CRERangeException("max - min must be greater than 0", t);
1159-
}
1160-
long rand = java.lang.Math.abs(r.nextLong());
1161-
long i = (rand % (range)) + min;
1156+
long rand = r.nextLong();
1157+
long i = Long.remainderUnsigned(rand, range) + min;
11621158

11631159
return new CInt(i, t);
11641160
}

0 commit comments

Comments
 (0)