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 2 of 2

Thread: Monte Carlo Method

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Monte Carlo Method

    Hello, I have this assignment that I don't understand at all. here are the instructions:

    Write a program that uses the Monte Carlo sampling method to estimate
    the average number of bottles of e-Boost someone would have to drink to
    win a prize. There is a 1 in 5 chance that a bottle cap will have a prize.

    and 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 montecarlomethod
    {
        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; January 20th, 2012 at 09:58 PM.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Monte Carlo Method

    What specifically don't you understand?

    For a start, what is your code doing? Is there any compiler or runtime errors? If so, please post the message. What outputs are you getting, and what inputs are you using?

    suggested reading:

    http://www.javaprogrammingforums.com...e-posting.html
    http://www.javaprogrammingforums.com...-get-help.html

Similar Threads

  1. Replies: 3
    Last Post: October 31st, 2011, 12:42 AM
  2. How do I call a method from the main method?
    By JavaStudent1988 in forum Java Theory & Questions
    Replies: 5
    Last Post: October 19th, 2011, 08:37 PM
  3. Help with toString method and an addObject method?
    By Camisado in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 12th, 2011, 07:00 AM
  4. Can i call init() method in destroy method.?
    By muralidhar in forum Java Servlet
    Replies: 1
    Last Post: October 22nd, 2010, 11:18 AM
  5. Monte Carlo Method
    By KiwiFlan in forum Loops & Control Statements
    Replies: 2
    Last Post: July 22nd, 2010, 07:47 PM