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

Thread: Dice Rolling Probability - How Do I Start It Off?

  1. #1
    Junior Member iAce's Avatar
    Join Date
    Dec 2012
    Location
    || ^_^ ||
    Posts
    19
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Dice Rolling Probability - How Do I Start It Off?

    Just a quick question on how to start off a code on this. The program is supposed to simulate tossing a pair of 2-sided dice and determining the percentage of times each possible combination of the dice is rolled. The actual assignment is using 11-sided dice but I just want a simple code to start off with. I want to make it into one loop using for, if, and nested loops. How would a code like this look?

    Things I Need
    Loop to increment through the possible sums of the dice
    for (int sum = 2; sum <= 4; sum++)

    Loop to throw dice given number of times
    for (int roll = 0; roll < totalRolls; roll++)

    Randomly generate values for two dice
    die1 = (int)(Math.random() * 10);
    die2 = (int)(Math.random() * 10);
    diceSum = die1 + die2;

    Check if the sum of dice is equal to the given sum


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Dice Rolling Probability - How Do I Start It Off?

    Not sure what the first loop (for(sum)) is supposed to do. Can you explain why sum starts at 2 and goes to 4?

    determining the percentage of times each possible combination of the dice is rolled.
    That sounds like the dice should be rolled 1000s of times, each diceSum counted and then the % computed.
    Say there are 3 different sums, and the dice were rolled 1000 times then the sums could be:
    250, 500, 250
    and the %s would be:
    25, 50, 25
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member iAce's Avatar
    Join Date
    Dec 2012
    Location
    || ^_^ ||
    Posts
    19
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Dice Rolling Probability - How Do I Start It Off?

    Quote Originally Posted by Norm View Post
    Not sure what the first loop (for(sum)) is supposed to do. Can you explain why sum starts at 2 and goes to 4?


    That sounds like the dice should be rolled 1000s of times, each diceSum counted and then the % computed.
    Say there are 3 different sums, and the dice were rolled 1000 times then the sums could be:
    250, 500, 250
    and the %s would be:
    25, 50, 25
    It's supposed to represent all the possible sums I could get from the dice. So the lowest possible number would be 2 and the highest would be 4.

    If the sum on the dice is equal to the sum, then it raises by one. If not, it just goes on to the next sum and it loops for how many times the user asks for it to be rolled.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Dice Rolling Probability - How Do I Start It Off?

    Is the problem: What % of rolls produces a value given by the user? The number of rolls to make is also given by the user.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member iAce's Avatar
    Join Date
    Dec 2012
    Location
    || ^_^ ||
    Posts
    19
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Dice Rolling Probability - How Do I Start It Off?

    Quote Originally Posted by Norm View Post
    Is the problem: What % of rolls produces a value given by the user? The number of rolls to make is also given by the user.
    It's similar to what you said. I need to display the probability of how many times a combination will occur depending on how many rolls the user input.

    For example, since I'm doing a 2-sided dice and let's say the user enters in 1 roll:

    2s: 25%
    3s: 50%
    4s: 25%

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Dice Rolling Probability - How Do I Start It Off?

    let's say the user enters in 1 roll:
    Not sure I understand what you mean by 1 roll.
    If a 2 is rolled then a 2 is rolled 1/1 = 100% of the time.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member iAce's Avatar
    Join Date
    Dec 2012
    Location
    || ^_^ ||
    Posts
    19
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Dice Rolling Probability - How Do I Start It Off?

    Quote Originally Posted by Norm View Post
    Not sure I understand what you mean by 1 roll.
    If a 2 is rolled then a 2 is rolled 1/1 = 100% of the time.
    I meant the user would enter in 1 as the number of rolls they want. Basically, it's like this:
    -Ask the user to input how many times the dice will be rolled.
    -Calculate the probability of each combination of dice.

    I'm confused as how to do that. The snippets I put above are just thoughts but I'm not sure if they will actually be used.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Dice Rolling Probability - How Do I Start It Off?

    I meant the user would enter in 1 as the number of rolls they want
    If there is only one roll, how do you compute the %? Normally there are 1000s of rolls.

    Calculate the probability of each combination of dice.
    Before you said the user would chose which sum of the dice the % was to be computed for.

    I don't think you understand the problem yet.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member iAce's Avatar
    Join Date
    Dec 2012
    Location
    || ^_^ ||
    Posts
    19
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Dice Rolling Probability - How Do I Start It Off?

    Quote Originally Posted by Norm View Post
    If there is only one roll, how do you compute the %? Normally there are 1000s of rolls.

    I don't think you understand the problem yet.
    Yeah, that was a bad example. If it was only 1 roll, then it would be 100% for one combination. I think 100 rolls would be better. So, how would I code this problem?

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Dice Rolling Probability - How Do I Start It Off?

    Before coding anything make a list of the steps (a design) the program should do to solve the problem.
    When you have a design then work on coding it.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member iAce's Avatar
    Join Date
    Dec 2012
    Location
    || ^_^ ||
    Posts
    19
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Dice Rolling Probability - How Do I Start It Off?

    Okay well here's what I have so far. I'm not sure if even this is correct. How do I display the number of times it matches for each sum?

    import java.util.Random;
    import java.util.Scanner;
    public class DiceProbability
    {
        public static void main(String[] args)
        {
            Scanner in = new Scanner(System.in);
            Random randNum = new Random();
     
            int match = 0;  //Number of times sum of dice matches the current sum
            int die1, die2;
            int totalRolls = 0;
            int diceSum = 0;
     
            System.out.print("How many times do you want to roll the dice? ");
            totalRolls = in.nextInt();        
     
            //Print heading for output table
            System.out.println("Sum of Dice      Probability");
     
            for (int sum = 2; sum <= 22; sum++)
            {
                for (int roll = 0; roll < totalRolls; roll++)
                {
                    die1 = randNum.nextInt(11);
                    die2 = randNum.nextInt(11);
                    diceSum = die1 + die2;  
                } 
     
                if (diceSum == sum)
                {
                   match++;
                }
                else
                {
                }
            }
     
            //After all throws, calculate percentage of throws that resulted 
            //in the given sum.
     
            //Print results 
            System.out.println("   2s:         " + match);
            System.out.println("   3s:         ");
            System.out.println("   4s:         ");
            System.out.println("   5s:         ");
            System.out.println("   6s:         ");
            System.out.println("   7s:         ");
            System.out.println("   8s:         ");
            System.out.println("   9s:         ");
            System.out.println("   10s:        ");
            System.out.println("   11s:        ");
            System.out.println("   12s:        ");
            System.out.println("   13s:        ");
            System.out.println("   14s:        ");
            System.out.println("   15s:        ");
            System.out.println("   16s:        ");
            System.out.println("   17s:        ");
            System.out.println("   18s:        ");
            System.out.println("   19s:        ");
            System.out.println("   20s:        ");
            System.out.println("   21s:        ");
            System.out.println("   22s:        "); 
     
        } //end main
    }//end class DiceProbability

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Dice Rolling Probability - How Do I Start It Off?

    How do I display the number of times it matches for each sum?
    Do the summing using an array and then you can print the contents of the array.

    Did you ever design the program (make a list of the steps)?

    What are the steps the posted code does to solve the problem?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Looking for help with java probability assignment.
    By Hasurat in forum Algorithms & Recursion
    Replies: 2
    Last Post: November 5th, 2012, 09:17 PM
  2. Need help with probability assignment
    By tai8 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 13th, 2012, 03:23 PM
  3. Need help coding Dice rolling simulation
    By DavidBowie in forum What's Wrong With My Code?
    Replies: 6
    Last Post: November 23rd, 2011, 02:03 PM
  4. Help with creating a dice rolling program in Java
    By lilmiss in forum Object Oriented Programming
    Replies: 4
    Last Post: October 26th, 2011, 09:27 PM
  5. [SOLVED] Dice Rolling Simulation
    By SnarkKnuckle in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 12th, 2011, 06:51 PM