I'm writing some functions in Clojure to act as helpers. Right now, I'm writing functions that make using Java's random generator easier to use.
Since random-float
and random-double
will be nearly identical except for the exact Java function called, I decided to try and factor out the identical parts and use the resulting function:
(import '[java.util Random])
(defn- random-floating [f min max ^Random rand-gen]
(let [r (f rand-gen)
spread (- max min)
rand (* spread r)]
(+ rand min)))
; Errors at ".nextFloat" and ".nextDouble"
(defn random-float [min max ^Random rand-gen]
(random-floating .nextFloat min max rand-gen))
(defn random-double [min max ^Random rand-gen]
(random-floating .nextDouble min max rand-gen))
(defn random-boolean [rand-gen]
(.nextBoolean rand-gen)) ; <<--- This works fine
The problem is, I'm getting "unresolved symbol" errors at both .nextFloat
and .nextDouble
, even though if I call them directly, they're found.
I tried qualifying them (Random/.nextFloat
and java.util.Random/.nextFloat
), and neither worked.