Skip to content

Commit

Permalink
Support 64-bit integers in rand()
Browse files Browse the repository at this point in the history
  • Loading branch information
PseudoKnight committed Dec 2, 2024
1 parent 354cce4 commit f552a20
Showing 1 changed file with 7 additions and 11 deletions.
18 changes: 7 additions & 11 deletions src/main/java/com/laytonsmith/core/functions/Math.java
Original file line number Diff line number Diff line change
Expand Up @@ -1115,9 +1115,9 @@ public Integer[] numArgs() {

@Override
public String docs() {
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"
+ " be less than max, and both numbers must be >= 0. This will return an integer. Alternatively, you can pass no arguments, and a random"
+ " double, from 0 to 1 will be returned.";
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 be less than max. This will return an integer."
+ " If no arguments are given, a random double from 0.0 to 1.0 (exclusive) will be returned.";
}

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

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

return new CInt(i, t);
}
Expand Down

0 comments on commit f552a20

Please sign in to comment.