Results 1 to 9 of 9
  1. #1
    Pugovitz is offline Member
    Join Date
    Sep 2009
    Posts
    3
    Rep Power
    0

    Default 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. #2
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    7

    Default

    you do realize that you could very well be getting values less than zero? check out the random api.

  3. #3
    Pugovitz is offline Member
    Join Date
    Sep 2009
    Posts
    3
    Rep Power
    0

    Default

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

    Default

    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. #5
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Quote Originally Posted by Pugovitz View Post
    ... 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. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,173
    Rep Power
    20

    Default

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

    Default

    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. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,173
    Rep Power
    20

    Default

    Quote Originally Posted by Pugovitz View Post
    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 View Post
    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. #9
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Quote Originally Posted by Pugovitz View Post
    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 View Post
    ..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 View Post
    ... 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 View Post
    .. Clearly I need to read up on random numbers a little.
    ++;

Similar Threads

  1. [SOLVED] Array index out of bounds exception
    By sruthi_2009 in forum New To Java
    Replies: 6
    Last Post: 03-23-2014, 11:16 AM
  2. Replies: 8
    Last Post: 04-19-2009, 06:50 PM
  3. Java ArrayList out of bounds exception
    By grahamb314 in forum New To Java
    Replies: 5
    Last Post: 11-22-2008, 08:21 PM
  4. [SOLVED] out of bounds exception help
    By soxfan714 in forum New To Java
    Replies: 21
    Last Post: 11-11-2008, 09:16 AM
  5. random numbers without random class`
    By carlos123 in forum New To Java
    Replies: 4
    Last Post: 01-17-2008, 11:44 PM

Tags for this Thread

Posting Permissions

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