samedi 13 janvier 2018

Having issues with dice probability program

Im am trying to make a program that calculates dice rolls using random digits and returns the probability of each sum happening for different numbers of rolls and trails. This code hower only results in probabilities of zero displaying. Seems to be an issue with the establishment of the match variable in the nested for loop. Wondering what I am doing wrong. Is it establishing the match variable based on the counter?

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

public class DiceProbability
{
public static void main(String[] args)
{
    Scanner in = new Scanner(System.in);
    Random randNumList = new Random();
    System.out.println("How many sides do the dice have: ");
    int diceSides = in.nextInt();
    System.out.println("How many times will the dice be rolled: ");
    int diceRolls = in.nextInt();
    int highestSum = diceSides * 2;
    int diceRoll1 = 0;
    int diceRoll2 = 0;
    int match = 0;
    int totalOfDiceRolls = 0;
    int counter = 0;
    int counter2 = 0;
    System.out.println("Rolls total   " + "Probability");
    for(counter=2;counter<=highestSum;counter ++)
  {
     for(counter2=1;counter2<=diceRolls;counter2 ++)
     {
     diceRoll1 = (randNumList.nextInt(11)+1);
     diceRoll2 = (randNumList.nextInt(11)+1);
     int totalOfRolls = diceRoll1 + diceRoll2;
     if(totalOfDiceRolls != counter)
        {
            match = match + 0;
        }
        else
        {
            match ++;
        }   
     }
     System.out.println(match);
     double probabilityOfSum = (match * 100 / diceRolls);
     System.out.println(counter + ":          " + probabilityOfSum);
     counter2 = 1;
    }
   } 
  }     




Aucun commentaire:

Enregistrer un commentaire