Page 1 of 1

Random Problem

Posted: Fri Jul 03, 2009 2:18 am
by klavierspieler21
I don't know Java much... I did mostly Scheme, and a bit of C.
Anybody care to explain why -2^31 failed?

Thx

Posted: Tue Jul 07, 2009 12:48 pm
by klavierspieler21
re:

Posted: Tue Jul 07, 2009 8:30 pm
by gfoot
The challenge is to find the specific number the JVM would have printed - not just any number that passes the test. It depends on the order of generation of numbers by Java's random number generator.

Posted: Wed Jul 08, 2009 7:28 pm
by klavierspieler21
i think you've mistaken with another challenge

public int bucketFromRandom(int randomNumber) {
int a[] = new int[10];
for (int i = 0; i < a.length; i++)
a = i * randomNumber;
int index = Math.abs(randomNumber) % a.length;
return a[index];
}

this has no reference to an actual random number. i just tried every int value possible, and -2^31 gave me an error, which i don't know why it's the answer.

Posted: Wed Jul 08, 2009 8:33 pm
by gfoot
Oh I see, sorry, I did get mixed up.

The reason for the error is that although -2^31 can be represented in an int, 2^31 cannot. So it's an error to call Math.abs on -2^31.

Posted: Tue Aug 18, 2009 1:35 am
by SinistraD
Thank you gfoot, I just couldn't get what's wrong with it, but now is all clear.

Posted: Fri Jul 22, 2011 10:35 am
by PainKeeper
i did it with this little code sniped. there some smaller / better way to do?
im not some java pro.

[code]
class Box
{
int zahl;
Box(int randomNumber) { zahl = randomNumber; }

int value() {
int a[] = new int[10];
for (int i = 0; i < a.length; i++)
a[i] = i * zahl;
int index = Math.abs(zahl) % a.length;
try { return a[index]; } catch (ArrayIndexOutOfBoundsException e)
{ System.out.println("Exeption: " + zahl); }
return a[index];
}

public static void main(String[] args)
{
int vol;
for ( int faktor = 1; faktor <= Integer.MAX_VALUE; faktor ++ ) {
Box myTest1 = new Box (faktor);
vol = myTest1.value();
}

}
}
[/code]

javac filename.class
java Box

output:

[quote]
Exeption: -2147483648
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -8
at Box.value(rndtest.java:12)
at Box.main(rndtest.java:20)
[/quote]

Posted: Fri Jul 22, 2011 3:02 pm
by DaymItzJack
PainKeeper wrote:i did it with this little code sniped. there some smaller / better way to do?
im not some java pro.
Knowing what gfoot said and putting in the value.

Also just looping from Integer.MIN_VALUE all the way to Integer.MAX_VALUE and doing Math.abs() is probably the easiest way.

Posted: Wed Jul 27, 2011 4:37 pm
by moose
@gfoot: Thank you very much. I simply made a for-loop over every possible input for bucketFromRandom, but I couldn't guess whats wrong.

Posted: Tue Dec 13, 2011 6:08 am
by rain1024
I debug in Netbeans IDE 7.0.1. And I found that this exception appear when
[1] int index = Math.abs(randomNumber) % a.length;
[2] return a[index];
with randomNumber = Integer.MIN_VALUE -> index = - 8 (I don't know why)
so exception appear when try access a[-8]

Posted: Tue Dec 13, 2011 8:34 am
by harvestsnow
Hello,

I suppose Math.abs(x) returns -x if x is negative and x otherwise.
In two's complement, -x is the same as ~x+1 (inverse all the bits of x and add 1).
But -2^31 is represented by 0x80000000; it stays the same when you apply the algorithm, as 0 does.
So --2^31 is still -2^31.
And as gfoot said, +2^31 has no possible representation in 32 bits two's complement system.
You can verify it with this code:

Code: Select all

public class Abs{
        public static void main(String args[]){
                if(args.length!=1){
                        System.out.println("Usage: java Abs number");
                        return;
                }
                int i=Integer.parseInt(args[0]);
                System.out.println("Input: " + i);
                System.out.println("Absolute: " + Math.abs(i));
                System.out.println("Negative: " + (-i));
        }
}
Add to that, the modulo operation keeps the sign of the left operand, and you have your answer.

I brute-forced it too, by the way.

Posted: Mon May 28, 2012 10:34 am
by avrrobot
Just googled the specs of Math.abs.