Skip to content

Instantly share code, notes, and snippets.

@AdamClements
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save AdamClements/f53cbce895e470d1935c to your computer and use it in GitHub Desktop.
Save AdamClements/f53cbce895e470d1935c to your computer and use it in GitHub Desktop.
Generate floats in a range for test.check generators
(def float
"Generates a floating point number which shrinks towards zero"
(fmap core/float ratio))
(defn float-in-range
"Generates a floating point number with a given start/end
point (inclusive)"
[range-start range-end]
(let [gen-float-0-to-1 (sized (fn [size]
;; We can't have 0 as our bound, would give NaN
(let [bound (max size 1)]
(fmap (fn [x] (/ x (core/float bound)))
(choose 0 bound)))))
range-width (- range-end range-start)]
(fmap (fn [x] (+ range-start (* x range-width)))
gen-float-0-to-1)))
@arinzeuba
Copy link

hi

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment