]> gerrit.simantics Code Review - simantics/sysdyn.git/commitdiff
Implement randomUniform and fix randomNumber in internal solver
authorjkauttio <jkauttio@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Wed, 27 May 2015 11:01:18 +0000 (11:01 +0000)
committerjkauttio <jkauttio@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Wed, 27 May 2015 11:01:18 +0000 (11:01 +0000)
refs #5878

git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@31359 ac1ea38d-2e2b-0410-8846-a27921b304fc

fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Environment.java

index f4f5c2943db152c0ec9f0b20cf9a2f1d722eaa69..0f6fc0d3d2d0a57f8ff41838969a1234a9cb57d3 100644 (file)
@@ -21,6 +21,7 @@ import java.util.HashMap;
 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
@@ -70,6 +71,16 @@ abstract class Fn1 implements Fn {
        \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
@@ -625,15 +636,38 @@ final public class Environment implements IEnvironment, ISystem {
                        }\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