mackanmorre
mackanmorre

Reputation: 145

Java, calculation of median and average when using Math.random

My program lets the user choose how many times he want to throw the dice, then the program prints out how many times he got each side of the dice.

So far everything is good, but then I have to calculate the median and the average of all these thrown dice, and I want both the average and the median to range from 1-6 corresponding to how many sides a dice has - how do I do that?

import java.util.Scanner;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class Uppgift4_5 
{
    public static void main(String[] args) 
    {
        Scanner inputReader = new Scanner(System.in);
        System.out.println("How many times do you want to throw the dice:");
        int amount = inputReader.nextInt();
        System.out.println("Antal försök:" + amount);

        Map<Integer, Integer> rolls = new HashMap<>();

        for (int i = 1; i < 7; i++) 
        {
            rolls.put(i, 0);
        }

        for (int i = 0; i < amount; i++) 
        {
            int value = (int) (Math.random() * 6 + 1);
            rolls.put(value, rolls.get(value) + 1);
        }

        for (Map.Entry<Integer, Integer> entry : rolls.entrySet()) 
        {
            System.out.println(("Antal kast som gav "+entry.getKey()) + ": " + entry.getValue());
        }
    }
}

The first for-loop initializes the keys 1 to 6 in the hashmap.

The second for-loop computes X number of dice throws and adds them to the hashmap.

The third for-loop iterates through the values in the hashmap and prints out the results.

Upvotes: 0

Views: 1592

Answers (3)

Bananan
Bananan

Reputation: 609

To calculate average you just have to get the sum of all rolls (rolls.entrySet(), Map.Entry::getKey(), Map.Entry::getValue()) and divide it by its amount.

To calculate median get the sum of all rolls + 1 and divide it by 2 - that's for odd amount of rolls. When there are even amount of rolls place all rolls in a ordered series, take (n-1)/2-th and n/2-th elements and divide by 2.

List<Integer> series = new ArrayList<>();
for (Map.Entry<Integer, Integer> entry : rolls.entrySet()) {
    for (int count = entry.getValue(); count > 0; count--) {
        series.add(entry.getKey());
    }
}
double median = 0;
if (series.size() % 2 == 0) {
    int sum = 0;
    for (int element : series) {
        sum += x;
    }
    median = sum / 2;
} else {
    Collections.sort(series);
    median = (series.get((series.size() - 1) / 2) + series.get(series.size() / 2)) / 2;
}

One more solution to calculate median for odd number of rolls is to take only (n-1)/2-th element from the series.

double median = 0;
Collections.sort(series)
if (series.size() % 2 == 0) {
    median = series.get((series.size() - 1) / 2);
} else {
    median = (series.get((series.size() - 1) / 2) + series.get(series.size() / 2)) / 2;
}

Upvotes: 2

Murtaza Khanjiwala
Murtaza Khanjiwala

Reputation: 1

For finding median you need to find which face of the dice appeared at (n+1)/2 for odd and at n/2 and n/2+1 for even. These are the medians in this case. So just store the faces which appear on the top in an array and find your median.

Upvotes: 0

Nishesh Pratap Singh
Nishesh Pratap Singh

Reputation: 2181

For calculating average you can use below code :

double average=0;
double total=0;
for (Map.Entry<Integer, Integer> entry : rolls.entrySet()) 
{
    total+=entry.getKey()*entry.getValue();
    System.out.println(("Antal kast som gav "+entry.getKey()) + ": " + entry.getValue());
}

average = total/amount;
System.out.println("Average : "+average);

Upvotes: 3

Related Questions