On Fri, Apr 30, 2010 at 8:56 PM, Lee Spector <lspec...@hampshire.edu> 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 this is only true in the sense that it 
> doesn't produce an error.


You're right.  Under Clojure 1.1.0, I ran the following:

(def count-if (comp count filter))
(count-if #(= 2147483647 %)
  (take 1000 (repeatedly #(rand-int 10000000000))))

Just running once, it shows that 2147483647 occured 802 times in a
thousand, which is... er, not so random.

This looks like a side effect of a really screwy coercion to int that
just shouldn't happen.  And in fact, in versions of clojure more
recent than commit d0e1eef2, you get a much more sensible error:

user=> (int 10000000000)
java.lang.IllegalArgumentException: Value out of range for int:
10000000000 (NO_SOURCE_FILE:0)

Of course, you might actually *want* a few really large random
numbers.  In clojure.contrib.math, we have 'round' which can help you
make some big numbers:

(take 10 (repeatedly #(round (rand 10000000000000))))

Of course, at some point, the resolution of the Double that Java
returns from its built-in 'rand' function will cause some problems for
the distribution of your really big numbers, since Java's 'rand'
returns a number between 0 and 1 that Clojure multiplies by the
parameter.  Then, I'd see what the incanter folks would recommend,
since they probably know more about big random numbers than I do.

Have fun!

-- 
Chris Riddoch

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to