Catch ArrayIndexOutOfBoundsExceptions in random.clj

This commit is contained in:
Jeremy Dormitzer 2018-04-01 10:05:22 -04:00
parent 1e98e2916d
commit 66e33cdcf9
No known key found for this signature in database
GPG Key ID: 04F17C0F5A32C320

View File

@ -10,10 +10,16 @@
(defn random
([max] (random 0 max))
([min max]
(+ (* (.nextFloat rng) (- max min)) min)))
(try
(+ (* (.nextFloat rng) (- max min)) min)
(catch ArrayIndexOutOfBoundsException e
(random min max)))))
(defn random-normal
([] (random-normal 0 1))
([mean dev]
(let [distribution (NormalDistribution. rng (double mean) (double dev))]
(.sample distribution))))
(try
(.sample distribution)
(catch ArrayIndexOutOfBoundsException e
(random-normal mean dev))))))