HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Dice roll program with statistics

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
withprogramrollstatisticsdice

Problem

This is a working program I made to roll dice a specified number of times, show how many times each number (2-12) occurred, and the percentage of the total rolls that each number got.

I'm trying to make improvements to the efficiency of my code. I've already found one, as noted beside int[] results on line 15. Are there any tools I could have used to make this code more efficient in terms of performance, or that could have saved me time? (Such as ArrayList, case/switch etc)

```
import java.util.Random;
import java.util.Scanner;
import java.text.DecimalFormat;
public class Dice {
public static void main(String[] args){
DecimalFormat df = new DecimalFormat("#.#"); //how to display percentage of results
Scanner sc = new Scanner(System.in);
System.out.println("How many times would you like to roll the dice?");
int rolls = sc.nextInt();
int[] counts = new int[rolls]; //creates an array of user-specified length (rolls)
sc.close();
for(int i = 0; i < counts.length; i++){ //populates said array with each roll of the dice
counts[i] = diceRoll();
}
int[] results = counters(counts); /*creates a new array with the returned array that counters(counts) gave us.
Seems redundant, but it is noticeably faster at high numbers to iterate through this array
than it is to access the counters method 3 times in the loop below (by using counters(counts) instead of results).*/

System.out.println("Roll\t\tCount\t\tFrequency"); //Headers for the results table
for(int i = 0; i < results.length; i++){ //iterates through our results array, prints how many times each number was rolled, along with its percentage of the total rolls
System.out.println((i+2)/to start the table at 2, since rolling a 0 is not possible/+"\t\t" + (results[i]) + "\t\t"+df.format(100.0*(results[i])/counts.length)+ "%");

Solution

It is nice that you have separate methods for throwing the dice and computing the counts. However, diceRoll can be simplified and counters supersimplified; like this

public static int diceRoll() { 
        Random rand = new Random();
        return rand.nextInt(6) + rand.nextInt(6) + 2;
    }

    public static int[] counters(int[] arr) { 
        int[] counterArray = new int[11];

        for (int dice : arr) {
            counterArray[dice - 2]++;
        }

        return counterArray;
    }


(Edit Also, the name of each method should (preferably) begin with a verb: counters \$\rightarrow\$ countResults and diceRoll \$\rightarrow\$ rollDice.)

Hope that helps.

Code Snippets

public static int diceRoll() { 
        Random rand = new Random();
        return rand.nextInt(6) + rand.nextInt(6) + 2;
    }

    public static int[] counters(int[] arr) { 
        int[] counterArray = new int[11];

        for (int dice : arr) {
            counterArray[dice - 2]++;
        }

        return counterArray;
    }

Context

StackExchange Code Review Q#123169, answer score: 2

Revisions (0)

No revisions yet.