Per Alex Osborne's reference to BigInteger constructors, here's a
random BigInteger function:
(defn rand-bigint [#^BigInteger bign, #^Random rnd]
(let [bits (inc (.bitLength bign))
bigr (BigInteger. bits rnd)]
(-> bign (.multiply bigr) (.shiftRight bits
--
You received this mes
Sorry, it just occurred to me after I hit send:
(defn huge-random-number [digits]
(BigDecimal. (apply str (take digits (repeatedly #(rand-int 10))
user=> (defn huge-random-number [digits]
(BigDecimal. (apply str (take digits (repeatedly #(rand-int 10))
#'user/huge-random-number
user=>
On Fri, Apr 30, 2010 at 8:56 PM, Lee Spector wrote:
>
> In an earlier thread, in which I learned (from Timothy Pratley) that (. (new
> java.util.Random) X) gives an error if X is a bignum, I said that at least
> Clojure's rand-int "does the right thing."
>
> Upon further investigation I see that
Lee Spector writes:
> Sorry, the expression in my first sentence should have been "(. (new
> java.util.Random) (nextInt X))", not "(. (new java.util.Random)
> X)". In any event the real question isn't about this call but about
> how Clojure's rand-int should handle bignum arguments and about how
Hmm, actually, that doesn't give numbers in the right range because of
the way the remainder is handled. You could round up the remainder to
the nearest power of two and then generate random coefficients using
rejection sampling but that's not as nice as a closed form solution.
Probably any of the
On Sat, May 1, 2010 at 7:25 PM, Lee Spector wrote:
> about how to write a real random bignum generator.
Let n be the bignum upper bound on the desired range. Find the
quotient q and remainder r of n by b = 2^31-1. Generate q random
numbers with upper bound b and one random number with upper bound
Sorry, the expression in my first sentence should have been "(. (new
java.util.Random) (nextInt X))", not "(. (new java.util.Random) X)". In any
event the real question isn't about this call but about how Clojure's rand-int
should handle bignum arguments and about how to write a real random big
In an earlier thread, in which I learned (from Timothy Pratley) that (. (new
java.util.Random) X) gives an error if X is a bignum, I said that at least
Clojure's rand-int "does the right thing."
Upon further investigation I see that this is only true in the sense that it
doesn't produce an err