Results 1 to 4 of 4
  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. 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 02:27 AM. Reason: Forgot to include program

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,788
    Rep Power
    7

    Default

    One way would be to iterate over the array to find the MAX value. Then iterate over the loop again and print out each number that has a count equal to MAX. That is slightly inefficient as you must iterate over the array twice.

    Another method would be to have a StringBuilder/StringBuffer. Keep track of the MAX count and each number that has a count equal to MAX add it to the buffer. If a number has a count greater than MAX update max, delete all values from the buffer and add your new number to the buffer.

    For example if you had: 1:4, 2:2, 3:4, 4:1, 5:7, 6:7
    Add 1 to buffer
    Store 4 in MAX
    2 has a count less than MAX - ignore
    3 has a count equal to MAX, add 3 to buffer
    ignore 4
    5 has a count greater than MAX, delete 1 and 3 from buffer, add 5 to buffer, update MAX
    6 has a count equal to MAX, add 6 to buffer
    Print out buffer

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

    Default

    Original poster -- please abide by the rules you agreed to on joining this forum and do not double post a question. Post it once in the best forum only.

  4. #4
    Kratos321's Avatar
    Kratos321 is offline Member
    Join Date
    May 2011
    Posts
    31
    Rep Power
    0

    Default

    @ Junky. thanks alot I think I can use your idea.
    @Fubarable. oh yeah sorry about that.

Similar Threads

  1. Problem in Finding the Mode of Dice Roll Outcomes? Help?
    By Kratos321 in forum Advanced Java
    Replies: 1
    Last Post: 05-11-2011, 03:21 AM
  2. 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
  3. HELP WITH DICE ROLL PROBLEM (netbeans)
    By IdrinkJava in forum New To Java
    Replies: 3
    Last Post: 03-14-2011, 03:55 PM
  4. Roll The dice
    By Subhanrukh in forum New To Java
    Replies: 8
    Last Post: 04-19-2010, 08:39 AM
  5. Roll dice class with three dices
    By nube07 in forum New To Java
    Replies: 4
    Last Post: 07-14-2008, 01:37 AM

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
  •