# Random numbers and an Out of Bounds exception teaming up to annoy me.

• 09-10-2009, 01:03 AM
Pugovitz
Random numbers and an Out of Bounds exception teaming up to annoy me.
I'm writing a program to test the Monty Hall problem. I have most of it figured out (though I'm sure I made it more difficult that I had to), I'm just getting an Out of Bounds Exception and I'm not sure why.

The code's kinda long so I'll just post the part bothering me.
Code:

``` import java.util.Random; public class Main {     public static void main(String[] args)     {         Random randomNum;         int doors[];         for (int i=0; i<100000; i++)         {             //put car behind random door             randomNum = new Random();             int a = (randomNum.nextInt())%3;             doors = new int[3];             for (int j=0; j<3; j++)                 doors[j]= 1;             doors[a]+= 1;                         //player chooses random door             int b = (randomNum.nextInt())%3;             doors[b]= doors[b]+1;```
a and b should randomly be 0, 1 or 2. Then I'm trying to add 1 to the number in doors[a] and doors[b], to tell me where the car is and which door the person chose.

If you don't know what the Monty Hall problem is, it's on Wikipedia, but it shouldn't really be necessary.

Thanks.
• 09-10-2009, 01:41 AM
emceenugget
you do realize that you could very well be getting values less than zero? check out the random api.
• 09-10-2009, 03:38 AM
Pugovitz
Well, I put a number inside the () in Random() and that seemed to work. I still don't know what exactly was going wrong before, but it's working now, so I'm good.
• 09-10-2009, 07:30 AM
rdtindsm
FWIW
This is a classic problem that will start arguments from people that just don't get it.

I presented it to some friends at work. One rolled his eyes into the back of his head, and gave me the correct answer, the two others immediately understood the explanation, the last one insisted that my computer simulation was wrong. Absolutely typical. I saw a poster that had, I believe, a PhD in some sort of scientific statistics get it wrong.
• 09-10-2009, 08:36 AM
r035198x
Quote:

Originally Posted by Pugovitz
... I still don't know what exactly was going wrong before, but it's working now, so I'm good.

• 09-10-2009, 12:08 PM
Tolls
As always, a suggestion.
Random stuff aside, why not use a boolean array? After all, the car is either behind a door (true) or not (false).
In addition, you won't have to do any initialising loops since:
boolean[] b = new boolean[3];
sets all the array values to false as a default.
• 09-10-2009, 04:30 PM
Pugovitz
Couldn't I still get a negative number even when I type "Random(3)"? And even though the number is negative, would the mod operator give negative answers? And I'm just trying to put the value into that array, shouldn't I still be able to do it, even if it's negative? Clearly I need to read up on random numbers a little.

Tolls: There's another variable to account for. There's a goat behind a random door too. I set the door with the goat to subtract one, and the door with the car and the door the player chose add one. If the door the player chose has a value of three, then the player wins.
• 09-10-2009, 04:37 PM
Tolls
Quote:

Originally Posted by Pugovitz
Couldn't I still get a negative number even when I type "Random(3)"? And even though the number is negative, would the mod operator give negative answers? And I'm just trying to put the value into that array, shouldn't I still be able to do it, even if it's negative? Clearly I need to read up on random numbers a little.

I'll save you a trip, since you're so close...use nextInt(n), which will give you an int between 0 and n-1.

Quote:

Originally Posted by Pugovitz
Tolls: There's another variable to account for. There's a goat behind a random door too. I set the door with the goat to subtract one, and the door with the car and the door the player chose add one. If the door the player chose has a value of three, then the player wins.

Ah good. I was hoping it wasn't a ailly "fear of booleans" thing.
:)

You want a tribool then!
false, true, FILE_NOT_FOUND...or should that be GOAT_NOT_FOUND?
• 09-10-2009, 09:31 PM
r035198x
Quote:

Originally Posted by Pugovitz
Couldn't I still get a negative number even when I type "Random(3)"?

You should verify that yourself by reading the API specs for the method.

Quote:

Originally Posted by Pugovitz
..And even though the number is negative, would the mod operator give negative answers?..

You should read up on the mod operator from Sun's Java tutorial.

Quote:

Originally Posted by Pugovitz
... And I'm just trying to put the value into that array, shouldn't I still be able to do it, even if it's negative?

You can put negative integers into an integer array but that's not all you are doing. You are doing doors[b] = doors[b]+1; Clearly you are accessing an index number b as well.

Quote:

Originally Posted by Pugovitz
.. Clearly I need to read up on random numbers a little.

++;