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

1. Member
Join Date
Sep 2009
Posts
3
Rep Power
0

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.
Java 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.

2. Senior Member
Join Date
Sep 2008
Posts
564
Rep Power
9
you do realize that you could very well be getting values less than zero? check out the random api.

3. Member
Join Date
Sep 2009
Posts
3
Rep Power
0
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.

4. Member
Join Date
Feb 2009
Posts
92
Rep Power
0
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.

5. Senior Member
Join Date
Aug 2009
Posts
2,388
Rep Power
10
Originally Posted by Pugovitz
... I still don't know what exactly was going wrong before, but it's working now, so I'm good.
Read emceenugget's post above again.

6. Moderator
Join Date
Apr 2009
Posts
13,130
Rep Power
23
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.

7. Member
Join Date
Sep 2009
Posts
3
Rep Power
0
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.

8. Moderator
Join Date
Apr 2009
Posts
13,130
Rep Power
23
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.

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?

9. Senior Member
Join Date
Aug 2009
Posts
2,388
Rep Power
10
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.

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.

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.

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

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•