user4718862
user4718862

Reputation:

Percentage math not coming out correctly for random dice roll

So I have this code I'm working on and it seems to run correctly except for the printing / showing percentage results. When the percentages are summed, I see that the total does not add up to 100. I feel like it may have something to do with casting, however, I don't see where the mistake is and have stepped through the code numerous times. If anyone could help me out as well as give me any tips reguarding structuring / any other noob stuff I should know, please do so! I'm a fairly new programmer and have less than half a year doing this, so like I said any tips will be appreciated. THANKS!

import java.util.Random;
import java.util.Scanner;

public class DiceRoller {

    public static void main(String[] args) {
        calculatePercentage();
    }

    //Get roll number from user
    static int getNumOfRolls(){
        Scanner input = new Scanner(System.in);

        System.out.println("How many times would you like to roll the dice?");
        int numOfRolls = input.nextInt();
        return numOfRolls;

    }
    //use object from class random to assign var value from 1 - 6 inclusive
    static int rollDice(){

        Random rand = new Random();
        int die = (rand.nextInt(6) + 1);

        return die;
    }

    static void printPercentage(int[] dieTotal, int numOfRolls){

        double totalPer = 0;
        double percent = 0;

        for(int i = 2; i < dieTotal.length; i++){

            int curNum = dieTotal[i];

            percent = ((curNum / (double)numOfRolls) * 100);
            totalPer += percent;
            System.out.printf("%d was rolled %.2f %% of the time. \n", i, percent);
        }

        System.out.println("Total percentage shown on the screen in: " + totalPer);
    }

    //store values of dice in an array. Call printPercent method defined above.
    static void calculatePercentage(){
        int numOfRolls = getNumOfRolls();

        int die1 = 0;
        int die2 = 0;
        int[] dieTotal = new int[13];

        for(int i = 0; i < numOfRolls - 1; i++){
            die1 = rollDice();
            die2 = rollDice();
            int total = die1 + die2;

            dieTotal[total]++;

        }

        printPercentage(dieTotal, numOfRolls);
    }
}

Upvotes: 2

Views: 273

Answers (2)

ShaneDemskie
ShaneDemskie

Reputation: 388

The error lies within your for loop condition statement in your calculatePercentage function.

Since you have the upper bounds set as i < numOfRolls -1, you'll only ever get the n-1 number of rolls. Make these changes below:

static void calculatePercentage(){
    int numOfRolls = getNumOfRolls();

    int die1 = 0;
    int die2 = 0;
    int[] dieTotal = new int[13];

    for(int i = 0; i < numOfRolls; i++){
        die1 = rollDice();
        die2 = rollDice();
        int total = die1 + die2;

        dieTotal[total]++;

    }

    printPercentage(dieTotal, numOfRolls);
}

Upvotes: 0

rgettman
rgettman

Reputation: 178303

You are rolling the dice one less than the number of times requested. E.g. if you enter 3, the the dice will be rolled only twice. The reason is your for loop condition:

for(int i = 0; i < numOfRolls - 1; i++){

This will stop the loop once it reaches 2 instead of 3. This is an "off by one" error. Try:

for(int i = 0; i < numOfRolls; i++){

This gives me:

Total percentage shown on the screen in: 100.0

Please note that with some values of numOfRolls, it still might not add up to 100% due to floating-point errors. E.g. 53 rolls gives me:

Total percentage shown on the screen in: 99.99999999999999

Upvotes: 4

Related Questions