Skip to content

Instantly share code, notes, and snippets.

@kevinherron
Created December 18, 2014 23:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kevinherron/b06151d59a7aad6bfb82 to your computer and use it in GitHub Desktop.
Save kevinherron/b06151d59a7aad6bfb82 to your computer and use it in GitHub Desktop.
public class RandomFunction extends AbstractFunction {
private static final Random RANDOM = new Random();
@Override
public QualifiedValue execute(Expression[] args) throws ExpressionException {
QualifiedValue qv = args[0].execute();
int bound = TypeUtilities.toInteger(qv.getValue());
synchronized (RANDOM) {
return new BasicQualifiedValue(RANDOM.nextInt(bound), qv.getQuality());
}
}
@Override
public String getArgDocString() {
return "random";
}
@Override
protected String getFunctionDisplayName() {
return "random";
}
@Override
public Class<?> getType() {
return Integer.class;
}
@Override
protected boolean validateNumArgs(int num) {
return num == 1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment