Results 1 to 3 of 3
  1. #1
    sciencewoona is offline Member
    Join Date
    Oct 2014
    Posts
    2
    Rep Power
    0

    Post Using loops for dice probability

    Hi, I'm new to posting here so please forgive me if this is a simple solution/ has already been answered.

    Anyway, I'm taking AP Computer Science with a virtual school and I'm having trouble with this assignment. The instructions are to "write a program to simulate tossing a pair of 11-sided dice and determine the percentage of times each possible combination of the dice is rolled... then ask the user
    to enter the number of sides on a die" The code is all provided here, I just have to put it in the right spots. (Side note, I cannot use arrays.) Here's the original file I was given to work with:

    Java Code:
    import java.util.Random;
    import java.util.Scanner;
    
    public class DiceProbability
    {
        public static void main(String[] args)
        {
        	//Declare and initialize variables and objects
    
            //Input: ask user for number of rolls and number of sides on a die
    
            //Print heading for output table
    
    
            //***************************************************************************************
            //Using nested loops, cycle through the possible sums of the dice.
            //Roll the dice the given number of times for each sum.
            //Count how many times the sum of the dice match the current sum being looked for.
            //***************************************************************************************
            //Loop to increment through the possible sums of the dice
            	//Loop to throw dice given number of times
            		//Randomly generate values for two dice
                	//Check if the sum of dice is equal to the given sum
                //After all throws, calculate percentage of throws that resulted in the given sum
                //Print results
    
    
    
    // Use the code below to complete the program. All statements are complete and when placed in the proper order, the program will work!
    // Be sure to indent code properly and include documation for each section..
    System.out.println("    " + sum + "\t\t" + probability );
    System.out.print("Please enter the number of sides on a die: ");
    System.out.print("Please enter the number of rolls: ");
    System.out.print("\n Sum of dice \t Probability \n");
    Scanner in = new Scanner(System.in);
    Random randNum = new Random();
    probability = (double)match / numRolls * 100;
    numSides = in.nextInt();
    numRolls = in.nextInt();
    match++;
    match = 0;
    int sum = 0;
    int numSides = 0;
    int numRolls = 0;
    int match = 0; //Number of times sum of dice matches the current sum
    int die2 = 0;
    int die1 = 0;
    double probability = 0.0;
    die2 = randNum.nextInt( numSides ) +1;
    die1 = randNum.nextInt( numSides ) +1;
    
    if ( die1 + die2 == sum )
    {
    } // end if
    
    for( sum = 2; sum <= (numSides * 2); sum++ )
    {
    } //end for
    
    for( int roll  = 0; roll < numRolls; roll++ )
    {
    } //end for
    
    
        } //end main
    }//end class DiceProbability
    So I worked with it for some time and came up with this:
    Java Code:
    public class DiceProbability1
    {
        public static void main(String[] args)
        {
            //Declare and initialize variables and objects
            int sum = 0;
            int numSides = 0;
            int numRolls = 0;
            int match = 0; //Number of times sum of dice matches the current sum
            int die2 = 0;
            int die1 = 0;
            double probability = 0.0;
            Scanner in = new Scanner(System.in);
    
            //Input: ask user for number of rolls and number of sides on a die
            System.out.print("Please enter the number of sides on a die: ");
            numSides = in.nextInt();
            System.out.print("Please enter the number of rolls: ");
            numRolls = in.nextInt();
           
            //Print heading for output table
            System.out.print("\n Sum of dice \t Probability \n");
            
    
            //***************************************************************************************
            //Using nested loops, cycle through the possible sums of the dice.
            //Roll the dice the given number of times for each sum.
            //Count how many times the sum of the dice match the current sum being looked for.
            //***************************************************************************************
            //Loop to increment through the possible sums of the dice
            //Loop to throw dice given number of times
            //Randomly generate values for two dice
            //Check if the sum of dice is equal to the given sum
            //After all throws, calculate percentage of throws that resulted in the given sum
            //Print results
            
            Random randNum = new Random();
            
            for( sum = 2; sum <= (numSides * 2); sum++ )
            {
                    match = 0;
                    for( int roll  = 0; roll < numRolls; roll++ )
                    {
                        die1 = randNum.nextInt( numSides ) +1;
                        die2 = randNum.nextInt( numSides ) +1;
                        if ( die1 + die2 == sum )
                        {
                            match++;
                        } // end if
                        probability = (double)match / numRolls * 100;
                        System.out.println("    " + sum + "\t\t" + probability );
                    } //end for
            } //end for
    
        } //end main
    }//end class DiceProbability
    When I run it, I get something like this (just a part of my output):
    Please enter the number of sides on a die: 6
    Please enter the number of rolls: 2

    Sum of dice Probability
    2 0.0
    2 0.0
    3 0.0
    3 0.0
    4 0.0
    4 0.0
    5 0.0
    5 0.0
    6 50.0
    6 50.0
    7 0.0
    7 0.0

    Now I don't really understand the concept in the first place as the lesson was extremely uninformative, so I don't know what went wrong here. Please help! (By the way, I'm using BlueJ, not sure if that's relevant.)

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: Using loops for dice probability

    You're printing the sum and the probability at each and every roll; you don't want that; you want to print those numbers after all rolls are done (hint: move lines #50 and $51 after line #52).

    kind regards,

    Jos

    ps. and increase the number of rolls for better accuracy.
    Build a wall around Donald Trump; I'll pay for it.

  3. #3
    sciencewoona is offline Member
    Join Date
    Oct 2014
    Posts
    2
    Rep Power
    0

    Default Re: Using loops for dice probability

    Quote Originally Posted by JosAH View Post
    You're printing the sum and the probability at each and every roll; you don't want that; you want to print those numbers after all rolls are done (hint: move lines #50 and $51 after line #52).

    kind regards,

    Jos

    ps. and increase the number of rolls for better accuracy.
    Yes that's it, thank you so much!

Similar Threads

  1. New to Java please help!!! arrays loops while loops
    By Silver22 in forum New To Java
    Replies: 3
    Last Post: 05-29-2014, 02:11 AM
  2. How to put dice on a JPanel?
    By Ivan in forum New To Java
    Replies: 4
    Last Post: 09-13-2013, 06:47 PM
  3. probability- sum of 2 cubes
    By Mapisto in forum New To Java
    Replies: 6
    Last Post: 12-01-2011, 11:28 PM
  4. Replies: 20
    Last Post: 03-22-2011, 05:02 AM
  5. Replies: 4
    Last Post: 10-04-2010, 09:01 AM

Posting Permissions

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