+(defun random (n)
+ "RANDOM N => RANDOM-NUMBER
+
+ARGUMENTS AND VALUES:
+
+ N: an integer, the upper bound of the random
+ RANDOM-NUMBER: an integer, the random result
+
+DESCRIPTION:
+
+ Returns a random number strictly closer to zero than N.
+
+ If number is positive, returns a random integer greater than or equal to 0,
+ but strictly less than number.
+
+ If number is negative, returns a random integer less than or equal to 0,
+ but strictly greater than number.
+
+ If number is zero, the result is always 0.
+
+ See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#random"
+ (coerce (clnl-random:next-long (truncate n)) 'double-float))
+
+(defun random-xcor ()
+ "RANDOM-XCOR => RANDOM-NUMBER
+
+ARGUMENTS AND VALUES:
+
+ RANDOM-NUMBER: a float, the random result
+
+DESCRIPTION:
+
+ Returns a random floating point number in the allowable range of turtle
+ coordinates along the x axis.
+
+ These range from min-pxcor - 0.5 (inclusive) to max-pxcor + 0.5 (exclusive)
+
+ See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#random-cor"
+ (let
+ ((min (- (min-pxcor) 0.5d0))
+ (max (+ (max-pxcor) 0.5d0)))
+ (+ min (clnl-random:next-double (- max min)))))
+
+(defun random-ycor ()
+ "RANDOM-YCOR => RANDOM-NUMBER
+
+ARGUMENTS AND VALUES:
+
+ RANDOM-NUMBER: a float, the random result
+
+DESCRIPTION:
+
+ Returns a random floating point number in the allowable range of turtle
+ coordinates along the y axis.
+
+ These range from min-pycor - 0.5 (inclusive) to max-pycor + 0.5 (exclusive)
+
+ See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#random-cor"
+ (let
+ ((min (- (min-pycor) 0.5d0))
+ (max (+ (max-pycor) 0.5d0)))
+ (+ min (clnl-random:next-double (- max min)))))
+
+(defun one-of (list-or-agentset)
+ "ONE-OF LIST-OR-AGENTSET => RESULT
+
+ LIST-OR-AGENTSET: LIST | AGENTSET
+ RESULT: RANDOM-VALUE | RANDOM-AGENT | :nobody
+
+ARGUMENTS AND VALUES:
+
+ LIST: A list
+ AGENTSET: An agent set
+ RANDOM-VALUE: a value in LIST
+ RANDOM-AGENT: an agent if AGENTSET is non empty
+
+DESCRIPTION:
+
+ From an AGENTSET, returns a RANDOM-AGENT. If the agentset is empty, returns :nobody.
+ From a list, returns a RANDOM-VALUE. If the list is empty, an error occurs.
+
+ See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#one-of"
+ (cond
+ ((agentset-p list-or-agentset)
+ (let*
+ ((agentset-list (agentset-list list-or-agentset))
+ (length (length agentset-list)))
+ (if (zerop length) :nobody (nth (clnl-random:next-int length) agentset-list))))
+ ((listp list-or-agentset)
+ (let*
+ ((length (length list-or-agentset)))
+ (if (zerop length)
+ (error "one-of requires a nonempty list")
+ (nth (clnl-random:next-int length) list-or-agentset))))
+ (t (error "one-of requires a list or agentset"))))
+