7f37cc5260ec

Try to unfuck the random-range stuff for floats
[view raw] [browse files]
author Steve Losh <steve@stevelosh.com>
date Tue, 27 Dec 2016 00:15:55 -0500
parents 18e71146fb25
children c3e4c4c8afa0
branches/tags (none)
files losh.lisp

Changes

--- a/losh.lisp	Tue Dec 27 00:07:07 2016 -0500
+++ b/losh.lisp	Tue Dec 27 00:15:55 2016 -0500
@@ -229,6 +229,14 @@
 
 
 ;;;; Random -------------------------------------------------------------------
+(defun-inline epsilon (val)
+  (etypecase val
+    (integer 1)
+    (short-float short-float-epsilon)
+    (long-float long-float-epsilon)
+    (single-float single-float-epsilon)
+    (double-float double-float-epsilon)))
+
 (defun-inlineable randomp (&optional (chance 0.5))
   "Return a random boolean with `chance` probability of `t`."
   (< (random 1.0) chance))
@@ -260,11 +268,11 @@
 
 (defun-inlineable random-range-inclusive (min max)
   "Return a random number in [`min`, `max`]."
-  (+ min (random (1+ (- max min)))))
+  (+ min (random (+ (- max min) (epsilon min)))))
 
 (defun-inlineable random-range-exclusive (min max)
   "Return a random number in (`min`, `max`)."
-  (+ 1 min (random (- max min 1))))
+  (+ (epsilon min) min (random (- max min (epsilon min)))))
 
 (defun random-around (value spread)
   "Return a random number within `spread` of `value`."