Skip to content

Instantly share code, notes, and snippets.

@arinzeuba
Forked from AdamClements/float-generators.clj
Last active August 29, 2015 14:06
Show Gist options
  • Save arinzeuba/170b7a8616cba1fb6aae to your computer and use it in GitHub Desktop.
Save arinzeuba/170b7a8616cba1fb6aae to your computer and use it in GitHub Desktop.
(def gen-float
"Generates a float between 0.0 and 1.0 bounded by size"
(gen/sized (fn [size]
(let [size (max size 1)] ; We can't have 0 as our bound,
; would give NaN
(gen/fmap (fn [x] (/ x (float size)))
(gen/choose 0 size))))))
(defn gen-float-in-range
"Generates a float with a given start/end point"
[start end]
(let [scale (- end start)]
(gen/fmap (fn [x] (+ start (* x scale))) gen-float)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment