diff --git a/src/net/sourceforge/plantuml/tim/TContext.java b/src/net/sourceforge/plantuml/tim/TContext.java index 7f789424a..af2db5c95 100644 --- a/src/net/sourceforge/plantuml/tim/TContext.java +++ b/src/net/sourceforge/plantuml/tim/TContext.java @@ -122,7 +122,7 @@ import net.sourceforge.plantuml.tim.stdlib.Lower; import net.sourceforge.plantuml.tim.stdlib.Newline; import net.sourceforge.plantuml.tim.stdlib.Now; import net.sourceforge.plantuml.tim.stdlib.Ord; -import net.sourceforge.plantuml.tim.stdlib.Random; +import net.sourceforge.plantuml.tim.stdlib.RandomFunction; import net.sourceforge.plantuml.tim.stdlib.RetrieveProcedure; import net.sourceforge.plantuml.tim.stdlib.ReverseColor; import net.sourceforge.plantuml.tim.stdlib.ReverseHsluvColor; @@ -209,7 +209,7 @@ public class TContext { functionsSet.addFunction(new LogicalNor()); functionsSet.addFunction(new LogicalNxor()); functionsSet.addFunction(new Ord()); - functionsSet.addFunction(new Random()); + functionsSet.addFunction(new RandomFunction()); // %standard_exists_function // %str_replace // !exit diff --git a/src/net/sourceforge/plantuml/tim/stdlib/Random.java b/src/net/sourceforge/plantuml/tim/stdlib/RandomFunction.java similarity index 75% rename from src/net/sourceforge/plantuml/tim/stdlib/Random.java rename to src/net/sourceforge/plantuml/tim/stdlib/RandomFunction.java index 51e0accdf..915779c9c 100644 --- a/src/net/sourceforge/plantuml/tim/stdlib/Random.java +++ b/src/net/sourceforge/plantuml/tim/stdlib/RandomFunction.java @@ -36,6 +36,7 @@ package net.sourceforge.plantuml.tim.stdlib; import java.util.List; import java.util.Map; +import java.util.Random; import java.util.Set; import net.sourceforge.plantuml.tim.EaterException; @@ -46,22 +47,35 @@ import net.sourceforge.plantuml.tim.TMemory; import net.sourceforge.plantuml.tim.expression.TValue; import net.sourceforge.plantuml.utils.LineLocation; -public class Random extends SimpleReturnFunction { +public class RandomFunction extends SimpleReturnFunction { public TFunctionSignature getSignature() { - return new TFunctionSignature("%random", 1); + return new TFunctionSignature("%random", 2); } public boolean canCover(int nbArg, Set namedArgument) { - return nbArg == 1; + return nbArg == 0 || nbArg == 1 || nbArg == 2; } + final Random random = new Random(); + public TValue executeReturnFunction(TContext context, TMemory memory, LineLocation location, List values, Map named) throws EaterException, EaterExceptionLocated { - if (values.get(0).isNumber()) { - final int limit = values.get(0).toInt(); - return TValue.fromInt((int) (Math.random() * limit)); + switch (values.size()) { + case 0: + return TValue.fromInt(random.nextInt(2)); + + case 1: + final Integer mx = values.get(0).toInt(); + return TValue.fromInt(random.nextInt(mx)); + + case 2: + final Integer min = values.get(0).toInt(); + final Integer max = values.get(1).toInt(); + return TValue.fromInt(random.nextInt(max - min) + min); + + default: + throw EaterException.located("Error on Random: Too many argument"); } - return TValue.fromInt(0); } }