> (defn gen-rands [max-val] ...
By the way, you could shorten this to:
(defn gen-rands [max-val]
(take 100 (repeatedly #(rand-int max-val))))
> Is there anything I've done wrong that would result in me getting
> 2147483647 so many times? Thanks in advance!
rand-int says it returns "an integer"; this seems to mean a 32-bit
Java int.
user> Integer/MAX_VALUE
2147483647
With a bit of testing, it seems like behind-the-scenes Clojure is
actually generating a number between 1 and the number you pass it;
then, it returns max(this value, Integer/MAX_VALUE).
For the time-being, you could try something like:
user> (def *random* (java.util.Random.))
#'user/*random*
user> (defn my-rand-int [max-val]
(let [bit-length (.bitLength (BigInteger. (str max-val)))]
(loop []
(let [x (BigInteger. bit-length *random*)]
(if (< x max-val)
x
(recur))))))
#'user/my-rand-int
user> (my-rand-int
123134202193481203941234192384712938471239841237489123481234812304902349820394812304213481203498)
8782878410281127624673886378825727079616630029207448645335947215751199693217517042830615540409
-Jason
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---