import java.util.HashSet;\r
import java.util.Iterator;\r
import java.util.Map;\r
+import java.util.Random;\r
import java.util.Set;\r
import java.util.TreeMap;\r
\r
\r
}\r
\r
+abstract class FnRandom extends Fn1 {\r
+ \r
+ protected Random random = null;\r
+\r
+ public FnRandom(int pCount) {\r
+ super(pCount);\r
+ }\r
+ \r
+}\r
+\r
final public class Environment implements IEnvironment, ISystem {\r
\r
final Map<String,Object> named = new HashMap<String,Object>();\r
}\r
\r
});\r
- model.functions.put("randomNumber", new Fn1(2) {\r
+ model.functions.put("randomNumber", new FnRandom(2) {\r
\r
@Override\r
public Object evaluate(IEnvironment environment, int argc) {\r
- @SuppressWarnings("unused")\r
Double seed = (Double)environment.getValue(0);\r
- @SuppressWarnings("unused")\r
- Double time = (Double)environment.getValue(1);\r
- return Math.random();\r
+ \r
+ if (random == null) {\r
+ random = new Random(Double.doubleToLongBits(seed));\r
+ }\r
+ \r
+ return random.nextDouble();\r
+ }\r
+ \r
+ });\r
+ model.functions.put("randomUniform", new FnRandom(4) {\r
+ \r
+ @Override\r
+ public Object evaluate(IEnvironment environment, int argc) {\r
+ Double min = (Double)environment.getValue(0);\r
+ Double max = (Double)environment.getValue(1);\r
+ Double seed = (Double)environment.getValue(2);\r
+ \r
+ // the fourth parameter should be "time" and is only necessary\r
+ // because sysdyn treats all functions as side-effect-free and\r
+ // does not evaluate a function "needlessly" if the parameters\r
+ // do not change. the parameter does not actually do anything\r
+ // though so we can safely ignore it here\r
+ \r
+ if (random == null) {\r
+ random = new Random(Double.doubleToLongBits(seed));\r
+ }\r
+ return random.nextDouble() * (max - min) + min;\r
}\r
\r
});\r