Add RNG
authorFrank Duncan <frank@kank.net>
Sat, 30 May 2015 17:47:10 +0000 (12:47 -0500)
committerFrank Duncan <frank@kank.net>
Sat, 30 May 2015 17:47:10 +0000 (12:47 -0500)
bin/runcmd.scala
src/main/cl-nl.asd
src/main/random.lisp [new file with mode: 0644]

index 916a56cbfaf4e4eb71743d47dc71b0f09c6e54ab..1e7987b94d5c8055d7012a8baac4be40b16fe547 100755 (executable)
@@ -22,6 +22,7 @@ import org.nlogo.util.Utils.url2String
 val workspace = HeadlessWorkspace.newInstance
 workspace.silent = true
 workspace.openFromSource(url2String(api.ModelReader.emptyModelPath))
 val workspace = HeadlessWorkspace.newInstance
 workspace.silent = true
 workspace.openFromSource(url2String(api.ModelReader.emptyModelPath))
+/*
 workspace.runCompiledCommands(new api.SimpleJobOwner("test", workspace.world.mainRNG, api.AgentKind.Observer), workspace.compileCommands("resize-world -2 2 -2 2 crt 1", api.AgentKind.Observer))
 mirror.Mirrorables.allMirrorables(workspace.world).map( x => {
   System.out.print("(")
 workspace.runCompiledCommands(new api.SimpleJobOwner("test", workspace.world.mainRNG, api.AgentKind.Observer), workspace.compileCommands("resize-world -2 2 -2 2 crt 1", api.AgentKind.Observer))
 mirror.Mirrorables.allMirrorables(workspace.world).map( x => {
   System.out.print("(")
@@ -39,6 +40,15 @@ mirror.Mirrorables.allMirrorables(workspace.world).map( x => {
   })
   System.out.println(")")
   
   })
   System.out.println(")")
   
-  })
+  })*/
+
+workspace.runCompiledCommands(new api.SimpleJobOwner("test", workspace.world.mainRNG, api.AgentKind.Observer), workspace.compileCommands("random-seed 15", api.AgentKind.Observer))
+for(_ <- 1 to 40)
+  System.out.println(workspace.runCompiledReporter(new api.SimpleJobOwner("test", workspace.world.mainRNG, api.AgentKind.Observer), workspace.compileReporter("random-float 30")))
+
+val m = new org.nlogo.util.MersenneTwisterFast();
+m.setSeed(15);
+for(_ <- 1 to 40)
+  System.out.println(30d * m.nextDouble())
 
 workspace.dispose
 
 workspace.dispose
index 74b5393771e453b05bb42844dac0b320b4093a4d..9ef24e314b84a19f0a5165bbcc7ad4b37699cb08 100644 (file)
@@ -6,5 +6,6 @@
   :serial t
   :components ((:file "package")
                (:file "lex")
   :serial t
   :components ((:file "package")
                (:file "lex")
+               (:file "random")
                (:file "main"))
   :depends-on (:cl-ppcre))
                (:file "main"))
   :depends-on (:cl-ppcre))
diff --git a/src/main/random.lisp b/src/main/random.lisp
new file mode 100644 (file)
index 0000000..7fca5a1
--- /dev/null
@@ -0,0 +1,24 @@
+(defpackage #:cl-nl.random
+ (:use :common-lisp)
+ (:export :set-seed :next-int :next-double))
+
+(in-package #:cl-nl.random)
+
+; This is a wrapper around the very nice mersenne twister mt19937 to match
+; NetLogo's implementation that tries to match how java.util.Random works
+(defun set-seed (n)
+ (setf mt19937:*random-state* (mt19937::make-random-object :state (mt19937:init-random-state n))))
+
+(defun next-int (n)
+ (rem (ash (mt19937:random-chunk mt19937:*random-state*) -1) n))
+
+(defun next-double (&optional (n 1d0))
+ (let
+  ((y (mt19937:random-chunk mt19937:*random-state*))
+   (z (mt19937:random-chunk mt19937:*random-state*)))
+ (*
+  (/
+   (+ (ash (ash y -6) 27) (ash z -5))
+   (coerce (ash 1 53) 'double-float))
+  n)))