Results 1 to 2 of 2
  1. #1
    Kratos321's Avatar
    Kratos321 is offline Member
    Join Date
    May 2011
    Posts
    31
    Rep Power
    0

    Unhappy Problem in Finding the Mode of Dice Roll Outcomes? Help?

    Ok, so I wrote a program in "Ready to Program with JAVA Technology IDE" that generates and prints out a total of 50 numbers between 1 and 6 to simulate dice throw outcomes. I also used arrays to try and find the mode number as efficiently as possible, using the least amount of code.

    THE PROBLEM: ok so the problem is that when there is more than one mode, only the mode with the largest dice index will be set to the most occuring number as it is the one processed last. So dice roll of 6 would be mode even though dice roll of 5 occured the same number of times.

    So does anyone know how to fix this problem with the least amount of code. I tried but just couldn't find a way. So this is what I have so far:


    // The "DiceRoll" class.
    import java.awt.*;
    import hsa.Console;

    public class DiceRoll
    {
    static Console c; // The output console

    public static void main (String[] args)
    {
    c = new Console ();

    int rollNumber, i=0;
    int modeNumber, mode;

    int[] num = new int[6];
    for(i=0;i<6;i++){
    num[i]=0;
    }//end of for loop

    for (i=0;i<50;i++) {

    rollNumber = (int) (Math.random()*6)+1;
    c.print(rollNumber+"\t");
    num[rollNumber-1]++;

    }//end of for loop

    for (i=0;i<6;i++) {
    c.println("\nNumber of "+(i+1)+"'s rolled: "+ num[i]);
    }//end of for loop

    modeNumber = num[0];
    mode = 1;
    for(int j=1; j<6; j++){
    if (num[j]>modeNumber) {
    modeNumber = num[j];
    mode = j+1;
    }
    //if (num2l
    }
    c.println("\nThe mode is "+mode);



    } // main method
    } // DiceRoll class
    Last edited by Kratos321; 05-11-2011 at 03:04 AM. Reason: forgot to include problem

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. Need help with AI in roll dice game
    By shazakala in forum New To Java
    Replies: 3
    Last Post: 04-04-2011, 11:49 AM
  2. HELP WITH DICE ROLL PROBLEM (netbeans)
    By IdrinkJava in forum New To Java
    Replies: 3
    Last Post: 03-14-2011, 03:55 PM
  3. Roll The dice
    By Subhanrukh in forum New To Java
    Replies: 8
    Last Post: 04-19-2010, 08:39 AM
  4. Roll dice class with three dices
    By nube07 in forum New To Java
    Replies: 4
    Last Post: 07-14-2008, 01:37 AM
  5. Roll 2-Dice "Pig" Game Help
    By King8654 in forum AWT / Swing
    Replies: 7
    Last Post: 04-07-2008, 06:58 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •