Skip to content

Commit

Permalink
[bugfix] random number generator accepts any numeric seed
Browse files Browse the repository at this point in the history
  • Loading branch information
line-o committed Feb 13, 2020
1 parent 5738599 commit bb80848
Showing 1 changed file with 13 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.exist.xquery.functions.map.MapType;
import org.exist.xquery.value.*;

import java.util.Optional;
import java.util.Random;

import static org.exist.xquery.FunctionDSL.*;
Expand Down Expand Up @@ -38,13 +39,21 @@ public FnRandomNumberGenerator(final XQueryContext context, final FunctionSignat

@Override
public Sequence eval(final Sequence[] args, final Sequence contextSequence) throws XPathException {
final Random random;
if (args.length == 1 && !args[0].isEmpty()) {
random = new Random(args[0].itemAt(0).toJavaObject(long.class));
final Sequence arg = getArgument(0).eval(contextSequence);
Optional<Long> result;

if(arg.isEmpty()) {
result = Optional.empty();
} else {
random = new Random();
try {
result = Optional.of(arg.convertTo(Type.LONG).toJavaObject(long.class));
} catch(final XPathException e) {
result = Optional.empty();
}
}

final Random random = result.map(Random::new).orElse(new Random());

return buildResult(context, random);
}

Expand Down

0 comments on commit bb80848

Please sign in to comment.