From: Frank Duncan Date: Sat, 30 May 2015 17:47:10 +0000 (-0500) Subject: Add RNG X-Git-Tag: v0.0.0~20 X-Git-Url: https://code.consxy.com/gitweb/gitweb.cgi?a=commitdiff_plain;h=6c59eecb1168aa02dd44efa6f416a77117e968ce;p=clnl Add RNG --- diff --git a/bin/runcmd.scala b/bin/runcmd.scala index 916a56c..1e7987b 100755 --- a/bin/runcmd.scala +++ b/bin/runcmd.scala @@ -22,6 +22,7 @@ import org.nlogo.util.Utils.url2String 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("(") @@ -39,6 +40,15 @@ mirror.Mirrorables.allMirrorables(workspace.world).map( x => { }) 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 diff --git a/src/main/cl-nl.asd b/src/main/cl-nl.asd index 74b5393..9ef24e3 100644 --- a/src/main/cl-nl.asd +++ b/src/main/cl-nl.asd @@ -6,5 +6,6 @@ :serial t :components ((:file "package") (:file "lex") + (:file "random") (:file "main")) :depends-on (:cl-ppcre)) diff --git a/src/main/random.lisp b/src/main/random.lisp new file mode 100644 index 0000000..7fca5a1 --- /dev/null +++ b/src/main/random.lisp @@ -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)))