Returns a random integer between 0 (inclusive) and n (exclusive).
(require '[clojure.set :as set]) ; random generation of unique series of random numbers from 0 to n-1 (defn unique-random-numbers [n] (let [a-set (set (take n (repeatedly #(rand-int n))))] (concat a-set (set/difference (set (take n (range))) a-set)))) user=> (unique-random-numbers 20) (0 1 3 6 7 8 9 12 14 16 17 19 2 4 5 10 11 13 15 18)
;; It'll allow you to supply an arg > Integer/MAX_VALUE, but the incidence of ;; integer overflow is proportional to how far you go over… (dotimes [n 10] (try (println (rand-int (* Integer/MAX_VALUE 2.0))) (catch ArithmeticException _ (println "A")))) A A 1854263459 83487894 629497223 A 1311192557 A A 2112963130 ;; The docstring uses the word "integer" to mean a Java integer, rather than a ;; math integer, confusingly unlike `integer?` ;; If you want to extend into the full Long range, repimplement like so… (defn rand-long [n] (long (rand n))) (dotimes [n 10] (try (println (rand-long (* Integer/MAX_VALUE 2.0))) (catch ArithmeticException _ (println "A")))) 2843734446 3705237344 2260635666 1353888982 2353595107 1511365943 2605201990 2533357711 2501246267 1330427847
Returns a random floating point number between 0 (inclusive) and n (default 1) (exclusive).
rand-int