Results 1 to 18 of 18
 11142009, 09:26 PM #1Member
 Join Date
 Nov 2009
 Posts
 8
 Rep Power
 0
use Math.random and then count occurrences
I have a project and I am LOST. I have to use Math.random to simulate (snd display the outcome) of dice 10,000 times. I can get that but then I have to use JOptionPane to diplay how many times each integer (16) appeared. I have tried many things but I am just lost. Here is how I am creating and displaying the 10,000 random numbers.
import javax.swing.JOptionPane;
public class Project2
{
public static void main(String[] p)
{
int face = 0;
String output = " ";
for(int f = 0; f<=10000; f++)
{
face = 1+(int)(6*Math.random());
output = output + face + " ";
if(f%25==0)
output = output + "\n";
}
System.out.println(output);
}
}
 11142009, 09:33 PM #2Senior Member
 Join Date
 Jul 2009
 Posts
 1,236
 Rep Power
 7
Use an Array, with 6 entries, to keep track each time a number occurs.
An don't forget to use the "Code" formatting tags when posting code.
 11142009, 10:59 PM #3Member
 Join Date
 Nov 2009
 Posts
 3
 Rep Power
 0
If you want to output this in Java Swing then instead of
Java Code:System.out.println(output);
Java Code:JOptionPane.showMessageDialog(null,output + "maybe some text?");
Java Code:import javax.swing.JOptionPane;
 11152009, 01:16 AM #4Member
 Join Date
 Nov 2009
 Posts
 8
 Rep Power
 0
The part that is REALLy throwing me off is, my proff wants the optput of the 10000 rolls in this format and then wants a dialog box with the number of 1s, 2s etc and then a box for user wants to continue?
Is there a way to still have this outout but then make it an array and count this was way?
 11152009, 02:26 AM #5Senior Member
 Join Date
 Jul 2009
 Posts
 1,236
 Rep Power
 7
my proff wants the optput of the 10000 rolls in this format
Is there a way to still have this outout but then make it an array and count this was way?

I wonder if the prof wants more than this such as the sum of several dice rolls. Can you post your exact assignment here?
 11152009, 05:15 AM #7Member
 Join Date
 Nov 2009
 Posts
 8
 Rep Power
 0
3. Write a program to simulate rolling dices as follows:
Use the method Math.random( ) to generate random integers bewteen 1 and 6 (see my class notes to review how to do this).
Then generate tenthousand numbers between 1 and 6. The calculate the number of 1 that has appeared, the number of 2 that has appeared, and so on. The final output must be on a dialog box (using JOptionPane). The process must be looped until the user decides to quit (you may want to look at Lab in Week 7). The output format (not the actual numbers) should like the following:
*****************************************
The dice has been rolled tenthousand times, here is the statitics
The number of 1s appeared is 2000
The number of 2s appeared is 2000
The number of 3s appeared is 2000
The number of 4s appeared is 2000
The number of 5s appeared is 1000
The number of 6s appeared is 1000
Do you want to try again (Y/N)?:

I think that you'll want to use an int array (say called intArray) of length 7 to hold your result. This way intArray[1] can hold the number of times 1 has been rolled, intArray[2] the number of times that 2 has been rolled,.... and intArray[6], the largest array element can hold the number of times 6 has been rolled. intArray[0] will not be used.
Then after every roll, check which number has been rolled and increase the int held in that corresponding array element. For instance if 3 has been rolled, simply call
Java Code:// either intArray[3]++; // or intArray[3] += 1; // or intArray[3] = intArray[3] + 1;
The reason an array is useful, is that you can just take the roll result, face, and use it as the array index:
Java Code:intArray[face]++;
 11162009, 01:27 AM #9Member
 Join Date
 Nov 2009
 Posts
 8
 Rep Power
 0
I am so close. I have everything except for the do/while choice to continue or not. I can't seem to put it in the right place. It seems like I get more error messages every time I try.
Any input?
Java Code:import javax.swing.JOptionPane; public class Project2 { public static void main(String[] args) { //create array of 10,000 random numbers int[] randomNumbers = new int[10000]; for(int i = 0; i < randomNumbers.length; i++) randomNumbers[i] =1 +(int)(Math.random() * 6); // Display the characters in the array 20 on each line for (int i = 0; i < randomNumbers.length; i++) { if ((i + 1) % 20 == 0) System.out.println(randomNumbers[i] + " "); else System.out.print(randomNumbers[i] + " "); } { //initialize count int[] counts = countInts(randomNumbers); displayIntCount(counts); } } //create new array to hold values of integer occurence and assign to new array public static int[] countInts(int[] ints) { int[] counts = new int[6]; for(int i = 1; i <=counts.length; i++) for(int j=0;j<ints.length;j++) if(ints[j] == i) counts[i1]++; return counts; } public static void displayIntCount(int[] counts) { //display the occurrences for (int i = 0; i < counts.length; i++) JOptionPane.showMessageDialog(null, "The number 1 occurs "+counts[i]+" times \nThe number 2 occurs "+counts[i+1]+" times \nThe number 3 occurs "+counts[i + 2]+ " times \nThe number 4 occurs " +counts[i+3]+ " times \nThe number 5 occurs " +counts[i + 4]+ " times \nThe number 6 occurs "+counts[i + 5]+ " times"); } }

Hmm, 5 replies and not one note of thanks...
much luck.
 11162009, 02:34 AM #11Member
 Join Date
 Nov 2009
 Posts
 8
 Rep Power
 0
I am very sorry!! I am very very greatful! I have been at this so long I can hardly think straight! I am so lost in this class I might never be found. I am very sorry If I offended you! That was not my intention.

Let's see where you think the do/while should go.
Also, are you really sure about this code here?
Java Code:for (int i = 0; i < counts.length; i++) JOptionPane.showMessageDialog(null, "The number 1 occurs " + counts[i] + " times \n" + "The number 2 occurs " + counts[i + 1] + " times\n" + "The number 3 occurs " + counts[i + 2] + " times\n" + "The number 4 occurs " + counts[i + 3] + " times\n" + "The number 5 occurs " + counts[i + 4] + " times\n" + "The number 6 occurs " + counts[i + 5] + " times");
Finally, when posting code, please only post indented code. You really don't want to make it hard for other's to read your code.
Much luck.
 11162009, 02:58 AM #13Member
 Join Date
 Nov 2009
 Posts
 8
 Rep Power
 0
thnak you, I will only post intended code from now on. Trying to figure out where the do/while loop goes is my problem. The only program we have written in the past with this has only had a single loop. I would have thought that all of the code should be in the looop, but this didn't seem to work. I tried putting it everywhere i could think of. I tried around just the final out put and it compiled but my joption dialog for the question of wether to continue didn't pop up and the look just ended. I am not sure I follow your other question about i?

Your code possibly ended with an error message, and if so, you will need to post that message.
regarding this code:
Java Code:for (int i = 0; i < counts.length; i++) JOptionPane.showMessageDialog(null, "The number 1 occurs " + counts[i] + " times \n" + "The number 2 occurs " + counts[i + 1] + " times\n" + "The number 3 occurs " + counts[i + 2] + " times\n" + "The number 4 occurs " + counts[i + 3] + " times\n" + "The number 5 occurs " + counts[i + 4] + " times\n" + "The number 6 occurs " + counts[i + 5] + " times");
Your for loop loops 6 times, the first time with i == 0, the next time with i == 1, the next with i == 2, and so on until i == 5 for the last loop.
So let's pretend that this code is running and walk through your code (something that you should learn to do).
The first time the loop runs, when i is 0, your joptionpane will display lines:
The number 1 occurs (here the contents of counts[0] since 0 + 0 = 0) times
The number 2 occurs (here the contents of counts[1] since 0 + 1 = 1) times
The number 3 occurs (here the contents of counts[2] since 0 + 2 = 2) times
The number 4 occurs (here the contents of counts[3] since 0 + 3 = 3) times
The number 5 occurs (here the contents of counts[4] since 0 + 4 = 4) times
The number 6 occurs (here the contents of counts[5] since 0 + 5 = 5) times
All well and good.
Then the loop loops again for i == 1. Now it will show these results
The number 1 occurs (here the contents of counts[1] since 1 + 0 = 1) times
The number 2 occurs (here the contents of counts[2] since 1 + 1 = 2) times
The number 3 occurs (here the contents of counts[3] since 1 + 2 = 3) times
The number 4 occurs (here the contents of counts[4] since 1 + 3 = 4) times
The number 5 occurs (here the contents of counts[5] since 1 + 4 = 5) times
The number 6 occurs (here the contents of counts[6] since 1 + 5 = 6) times
but counts[6] doesn't exist! And this this will throw an array index out of bounds error (look at your error messages and tell me that I'm wrong). So don't do this. Instead show six lines and don't loop here.
 11162009, 04:17 AM #15Member
 Join Date
 Nov 2009
 Posts
 8
 Rep Power
 0
Oh. I see what you mean now. I didn't think about it since I haven't gotten the loop running yet. Thanks!
 11162009, 07:51 AM #16Member
 Join Date
 Nov 2009
 Posts
 2
 Rep Power
 0
Just curious, is this for youming's project for CSCI 1236?
I'm not a stalker, I'm just in the class and read the problem you posted (my Math.random is generating right and I was looking for a solution).
 11162009, 05:29 PM #17Member
 Join Date
 Nov 2009
 Posts
 8
 Rep Power
 0
Hey! Yes it is and I have it working now. I had to email my TA for help. I was close but had my brackets screwed up and whoever was helping me on this forum REALLY helped. I had my final loop for my output messed up. What is going on with yours?
 11162009, 05:57 PM #18Member
 Join Date
 Nov 2009
 Posts
 2
 Rep Power
 0
I got # three working fine. My first two are having the same problem. 'm using for loops in the second method, but when I call it in the first method, it's not actually doing the loop or something, it just posts the same number over and over. I'm going to Youming's office hours today, but if he doesn't want to help (I'm pretty sure he hates us...) I'll try emailing Seth. How are your other ones?
Similar Threads

Array of instances using Math.random()
By xgi1008 in forum New To JavaReplies: 16Last Post: 01262011, 12:10 AM 
Math.random()
By Dieter in forum New To JavaReplies: 4Last Post: 09142009, 10:28 AM 
[SOLVED] Help with math.random
By tomiu in forum New To JavaReplies: 10Last Post: 12212008, 10:55 PM 
Math.Random
By Java Tip in forum Java TipReplies: 0Last Post: 11232007, 03:09 PM 
math.random function help
By katie in forum New To JavaReplies: 2Last Post: 08062007, 04:31 AM
Bookmarks