I'm getting an error from clojure-1.1.0 (with Java 1.6.0_16 under linux) that 
really has me scratching my head. Probably something obvious but I'm baffled 
and would love it if someone could point out what's going on.

The error that I get is:

Caused by: java.lang.IllegalArgumentException: n must be positive
        at java.util.Random.nextInt(Random.java:250)

And it seems pretty definitively to be coming from the call to nextInt  here:

(defn lrand-int
  "Return a random integer using the thread-local random generator."
  [n]
  (if (<= n 1)
    0
    (. thread-local-random-generator (nextInt n))))

The initial setting of thread-local-random-generator comes from:

(def thread-local-random-generator (new java.util.Random))

but the actual offending call is probably taking place in the context of a 
binding of thread-local-random-generator to a different java.util.Random object.

In any event, I don't see how the binding of thread-local-random-generator can 
matter.

This lrand-int gets called millions of times in my code, but it only ever 
produces the error when called from one particular place. Again, I don't see 
how the caller can matter because it sure looks to me like the argument to 
nextInt can only be positive no matter where lrand-int is being called... But 
the fact remains that I can eliminate the error (which takes a long, variable 
amount of time to manifest on a fast 16 core machine) if I change one 
particular call to lrand-int to the ordinary rand-int. The one problematic call 
to lrand-int is in here:

(defn perturb
  [pgm denom atom-generators]
  (if (seq? pgm)
    (let [zipper (zip/seq-zip pgm)
          points (count-points pgm)
          safe-denom (max 1 (math/abs denom))]
      (loop [z zipper i 0]
        (if (> i points)
          (zip/root z)
          (if (seq? (zip/node z))
            (recur (zip/next z) (inc i))
            (if (zero? (lrand-int safe-denom))
              (recur (zip/next (zip/replace z (random-atom atom-generators))) 
(inc i))
              (recur (zip/next z) (inc i)))))))
    (random-atom atom-generators)))

FWIW I'm using "atom" here in the Common Lisp sense, not in the Clojure sense, 
not that this should be relevant. Note that I'm even making sure (I think!) 
that the argument passed to lrand-int is at least 1, and then it gets checked 
again inside of lrand-int before being passed to nextInt.

If I change the call to lrand-int to rand-int in perturb then I get no errors. 
But I'd rather stick with lrand-int both to avoid contention for Random objects 
and because I do some patching of lrand-int in contexts where I don't want 
randomness (but I've turned that off for now anyway to track down this problem).

Any comments would be greatly appreciated.

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

Reply via email to