Results 1 to 4 of 4
- 05-11-2011, 03:23 AM #1
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 classLast edited by Kratos321; 05-11-2011 at 03:27 AM. Reason: Forgot to include program
- 05-11-2011, 03:38 AM #2
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
-
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.
- 05-11-2011, 10:07 PM #4
Similar Threads
-
Problem in Finding the Mode of Dice Roll Outcomes? Help?
By Kratos321 in forum Advanced JavaReplies: 1Last Post: 05-11-2011, 04:21 AM -
Need help with AI in roll dice game
By shazakala in forum New To JavaReplies: 3Last Post: 04-04-2011, 12:49 PM -
HELP WITH DICE ROLL PROBLEM (netbeans)
By IdrinkJava in forum New To JavaReplies: 3Last Post: 03-14-2011, 04:55 PM -
Roll The dice
By Subhanrukh in forum New To JavaReplies: 8Last Post: 04-19-2010, 09:39 AM -
Roll dice class with three dices
By nube07 in forum New To JavaReplies: 4Last Post: 07-14-2008, 02:37 AM
Bookmarks