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 bignum generator.
-Lee On Apr 30, 2010, at 10: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 this is only true in the sense that it > doesn't produce an error. > > In fact it does something that seems pretty odd to me, at least in Clojure > 1.1.0 / Java 1.6.0_17, which is all I've tried. It does seem to return a > random integer between 0 and 2^31-1 but it rather frequently returns exactly > 2^31-1 (which is 2147483647): > > user=> (rand-int 10000000000) > 2035176337 > user=> (rand-int 10000000000) > 2147483647 > user=> (rand-int 10000000000) > 2147483647 > user=> (rand-int 10000000000) > 2147483647 > user=> (rand-int 10000000000) > 1859741466 > user=> (rand-int 10000000000) > 2147483647 > user=> > > Looking at the source I see that it calls int which overflows with bignums > (whether by design or not I don't know): > > user=> (int 2147483648) > -2147483648 > > I don't immediately see why this would would cause rand-int to produce > 2147483647 so many times... maybe it's somehow an interaction with the > resolution of the float from the call to rand? In any event I guess this is > somehow the source of the behavior. > > Anyway, for my present application this doesn't matter much -- I'm just > punting and using 2147483647 as the limit if I'm given a bignum -- but: > > - Mightn't rand-int be written to really do the right thing and return a > random integer between 0 (inclusive) and n (exclusive), even if n is a bignum? > > - If there's a reason that rand-int shouldn't do this then is there another > straightforward way get the same effect, maybe with some java library that I > don't know about? > > Thanks, > > -Lee > > > -- > Lee Spector, Professor of Computer Science > School of Cognitive Science, Hampshire College > 893 West Street, Amherst, MA 01002-3359 > lspec...@hampshire.edu, http://hampshire.edu/lspector/ > Phone: 413-559-5352, Fax: 413-559-5438 > > Check out Genetic Programming and Evolvable Machines: > http://www.springer.com/10710 - http://gpemjournal.blogspot.com/ > > -- > 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 -- Lee Spector, Professor of Computer Science School of Cognitive Science, Hampshire College 893 West Street, Amherst, MA 01002-3359 lspec...@hampshire.edu, http://hampshire.edu/lspector/ Phone: 413-559-5352, Fax: 413-559-5438 Check out Genetic Programming and Evolvable Machines: http://www.springer.com/10710 - http://gpemjournal.blogspot.com/ -- 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