On 12/20/06, Peter Drake <[EMAIL PROTECTED]> wrote:

I tried creating a random number generator

java.util.Random rand = new java.util.Random()

and then asking it for a random int

rand.nextInt()

which I would then take modulo the board size to choose a random
point. Since ints in Java are signed, I would have to take the
absolute value of this int before the modulo operaton. (As in C/C++,
Java's % operator does not behave as modulo, in the mathematical
sense, when the first argument is negative. It might more accurately
be called "remainder".)

This almost always works, but once in a very great while I would get
a negative result anyway. I eventually hunted down the problem, which
is explained in the API for the java.lang.Math.abs() method:

"Note that if the argument is equal to the value of
Integer.MIN_VALUE, the most negative representable int value, the
result is that same value, which is negative."

Yikes!

So, I'll have to check the sign of my result and try again in those
rare (but not ignorably rare) instances where it is negative.


Or use (r & (-1>>1)) to obtain a nonnegative value (instead of abs(r)),
or use the nextInt(n) method...

-John
_______________________________________________
computer-go mailing list
[email protected]
http://www.computer-go.org/mailman/listinfo/computer-go/

Reply via email to