From 5ef9fa76a723cddfa63bb7f5fd025f1c0ba9f82d Mon Sep 17 00:00:00 2001
From: Chew Theam Yong <senatorzergling@gmail.com>
Date: Tue, 11 May 2010 21:39:16 +1200
Subject: [PATCH] Make RANDOM more Common-Lispy by adding support for float input arguments.

---
 src/macros.lisp |   17 ++++++++++++++---
 1 files changed, 14 insertions(+), 3 deletions(-)

diff --git a/src/macros.lisp b/src/macros.lisp
index 8aa9a63..314e215 100644
--- a/src/macros.lisp
+++ b/src/macros.lisp
@@ -53,9 +53,20 @@
           (and (numberp base) (= base 10) `(* (log ,n) (@ *math *log10e*)))
           `(/ (log ,n) (log ,base))))
     (sqrt (n) `((@ *math sqrt) ,n))
-    (random (&optional upto) (if upto
-                                 `(floor (* ,upto ((@ *math random))))
-                                 '((@ *math random)))))
+    (random (&optional upto)
+      (cond ((null upto)
+             '((@ *math random)))
+            ((floatp upto)
+             `(* ,upto ((@ *math random))))
+            ((integerp upto)
+             `(floor (* ,upto ((@ *math random)))))
+            (t ;; an expression
+             (with-ps-gensyms (max val)
+               `(let* ((,max ,upto)
+                       (,val (* ,max ((@ *math random)))))
+                  (if (= (floor ,max) ,max) ;; integerp
+                      (floor ,val)
+                      ,val)))))))
 
 (define-ps-symbol-macro pi (getprop *math '*pi*))
 
-- 
1.6.0.4

