Results 1 to 18 of 18
  1. #1
    iliak67 is offline Member
    Join Date
    Nov 2009
    Posts
    8
    Rep Power
    0

    Default 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 (1-6) 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);





    }

    }

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    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.

  3. #3
    Juo
    Juo is offline Member
    Join Date
    Nov 2009
    Posts
    3
    Rep Power
    0

    Default

    If you want to output this in Java Swing then instead of

    Java Code:
    System.out.println(output);
    You'd want something like

    Java Code:
    JOptionPane.showMessageDialog(null,output + "maybe some text?");
    ^^ would require you too import JOptionPane

    Java Code:
    import javax.swing.JOptionPane;

  4. #4
    iliak67 is offline Member
    Join Date
    Nov 2009
    Posts
    8
    Rep Power
    0

    Default

    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?

  5. #5
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    my proff wants the optput of the 10000 rolls in this format
    I doubt very much the prof wants to see 10k lines of output.

    Is there a way to still have this outout but then make it an array and count this was way?
    You update the values in the array with every roll of the dice.

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

    Default

    I wonder if the prof wants more than this such as the sum of several dice rolls. Can you post your exact assignment here?

  7. #7
    iliak67 is offline Member
    Join Date
    Nov 2009
    Posts
    8
    Rep Power
    0

    Default

    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 ten-thousand 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 ten-thousand 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)?:

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

    Default

    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]++;

  9. #9
    iliak67 is offline Member
    Join Date
    Nov 2009
    Posts
    8
    Rep Power
    0

    Default

    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[i-1]++;
     
    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");
    }
     
    }

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

    Default

    Hmm, 5 replies and not one note of thanks...
    much luck.

  11. #11
    iliak67 is offline Member
    Join Date
    Nov 2009
    Posts
    8
    Rep Power
    0

    Default

    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.

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

    Default

    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");
    Since i will loop from 0 to < 6, what will happen when you try to show counts[10] when counts only goes up to counts[5]?

    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.

  13. #13
    iliak67 is offline Member
    Join Date
    Nov 2009
    Posts
    8
    Rep Power
    0

    Default

    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?

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

    Default

    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");
    You understand that the counts array holds 6 integers, and that they are held at counts[0], counts[1], counts[2], counts[3], counts[4], and counts[5].

    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.

  15. #15
    iliak67 is offline Member
    Join Date
    Nov 2009
    Posts
    8
    Rep Power
    0

    Default

    Oh. I see what you mean now. I didn't think about it since I haven't gotten the loop running yet. Thanks!

  16. #16
    km03195 is offline Member
    Join Date
    Nov 2009
    Posts
    2
    Rep Power
    0

    Default

    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).

  17. #17
    iliak67 is offline Member
    Join Date
    Nov 2009
    Posts
    8
    Rep Power
    0

    Default

    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?

  18. #18
    km03195 is offline Member
    Join Date
    Nov 2009
    Posts
    2
    Rep Power
    0

    Default

    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

  1. Array of instances using Math.random()
    By xgi1008 in forum New To Java
    Replies: 16
    Last Post: 01-26-2011, 12:10 AM
  2. Math.random()
    By Dieter in forum New To Java
    Replies: 4
    Last Post: 09-14-2009, 10:28 AM
  3. [SOLVED] Help with math.random
    By tomiu in forum New To Java
    Replies: 10
    Last Post: 12-21-2008, 10:55 PM
  4. Math.Random
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-23-2007, 03:09 PM
  5. math.random function help
    By katie in forum New To Java
    Replies: 2
    Last Post: 08-06-2007, 04:31 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
  •