Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Monte Carlo Method

  1. #1
    Junior Member
    Join Date
    Jul 2010
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Monte Carlo Method

    I have to write a program using the Monte Carlo Method to estimate the average number of bottles of Boost someone would have to drink to win a prize.

    So far, I have prompted the user for the number of trials (1 trial = the times it takes until the winning cap is found). I have to conduct at least 1,000 trials. The problem is, I can't get it to print correctly. If I want 20 trials, it only prints about 5 numbers, and they are printed in increasing order.

    Here is what I have so far:
    //import the classes
    import java.io.IOException;
    import java.util.Scanner;
    import java.io.File;
    import java.util.Random;
    public class BottleCapPrize
    {
        public static void main(String[] args) throws IOException
        {
            Scanner in = new Scanner(System.in);
            Random rand = new Random();
     
            int count = 0;
     
            System.out.print("Number of Trials: ");
            int numTrials = in.nextInt();
     
           //randomly generate the value of the "guessing" cap
            int randBottleCap = rand.nextInt(5)+1;
     
            //Loop to increment through the number of trials    
            for(int i = 1; i <= numTrials; i++)
            {
                int winningCap = rand.nextInt(5)+1;
     
                //check to see if the "guessing" cap is equal to the winning cap
                if( randBottleCap != winningCap)
                {
                    count++;
                    randBottleCap = rand.nextInt(5)+1;
                }
                else
                {
                    System.out.println(count);
                }
     
            }
        }
    }
    Last edited by helloworld922; July 22nd, 2010 at 05:10 PM. Reason: Please surround your code with [highlight=Java]


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,328
    Thanks
    49
    Thanked 2,194 Times in 2,167 Posts

    Default Re: Monte Carlo Method

    If I want 20 trials, it only prints about 5 numbers, and they are printed in increasing order.
    What order are they supposed to be printed in?
    Have you tried debugging your program by printing out all the values of variables as they are generated and changed?
    Maybe you would see what the problem is.


    Please use code tags around your code to preserve formatting.

  3. #3
    Junior Member
    Join Date
    Jul 2010
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Monte Carlo Method

    Quote Originally Posted by Norm View Post
    What order are they supposed to be printed in?
    Have you tried debugging your program by printing out all the values of variables as they are generated and changed?
    Maybe you would see what the problem is.


    Please use code tags around your code to preserve formatting.
    They are supposed to be printed in any order, preferably random.

    I tried debugging my program, and I saw what the problem was. With a new arrangement, I was able to figure it out. Thanks!