Re: rand-int with bignums

2010-05-02 Thread RandyHudson
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

Re: rand-int with bignums

2010-05-01 Thread Chris Riddoch
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=>

Re: rand-int with bignums

2010-05-01 Thread Chris Riddoch
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

Re: rand-int with bignums

2010-05-01 Thread Alex Osborne
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

Re: rand-int with bignums

2010-05-01 Thread Per Vognsen
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

Re: rand-int with bignums

2010-05-01 Thread Per Vognsen
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

Re: rand-int with bignums

2010-05-01 Thread Lee Spector
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

rand-int with bignums

2010-04-30 Thread Lee Spector
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