Results 1 to 20 of 20
  1. #1
    abby0910 is offline Member
    Join Date
    Jul 2010
    Posts
    10
    Rep Power
    0

    Default Store Random numbers into Array

    I need to generate a random number between 1 and 6 to simulate rolling each dice. Keeping track of the result of each roll using an array that is indexed by the sum of the roll of the two dice. I have to output my result in a table which shows each value (ie. 2 – 12) and the number of times that value was rolled.
    So this is my code so far to generate random numbers.. now i basically dont know how do i store those numbers generated into array.. If anybody can help.. It would be great..!

    Thanks in advance..

    Here is the code:

    Java Code:
    package w2_2;
    
    import javax.swing.JOptionPane;
    import javax.swing.JTextArea;
    import java.util.Random;
    
    public class w2_2 {
    	
    	public static void main(String args[]){
    		Random dice = new Random();
    		int number;
    		
    		for (int counter=1; counter<=10; counter++){
    			number = 2+ dice.nextInt(11);
    			int store[];
    			store = new int[10];
    			store[counter]=number;
    		}
    		System.out.println(store[2]);
    	}
    
    }
    Last edited by Fubarable; 07-11-2010 at 03:36 AM. Reason: m

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

    Default

    Hello, and welcome to the forum. I hope you don't mind that I edited your code and added code tags which should help make your posted code retain its formatting and be more readable.

    To do this yourself, highlight your pasted code (please be sure that it is already formatted when you paste it into the forum; the code tags don't magically format unformatted code) and then press the code button, and your code will have tags.

    Another way to do this is to manually place the tags into your code by placing the tag [cod&#101;] above your pasted code and the tag [/cod&#101;] below your pasted code like so:

    Java Code:
    [cod&#101;]
      // your code goes here
      // notice how the top and bottom tags are different
    [/cod&#101;]

    _______________________________

    As for your problem, I would first and foremost create the array before the for loop and then fill the array inside the for loop.

    Best of luck, and again, welcome!

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default

    an array that is indexed by the sum of the roll of the two dice.
    That statement tells you several things:
    One, you need to generate 2 random numbers in the range of 1 to 6
    Two, you need to add the two numbers together.
    Three use that sum as the index to an array to count the number of times that total has occurred.
    To use the sum as an index to an array, the array should be big enough to be indexed by the numbers 2 to 12. if you subtract 2 to make it zero based, that would be 0 to 10 or 11 total numbers. So make your array that sized.

    You should be careful where you define the array so that it is in "scope" for the data collection and for the report.

  4. #4
    abby0910 is offline Member
    Join Date
    Jul 2010
    Posts
    10
    Rep Power
    0

    Default

    Thanks.. I will keep in mind about formatting.. So i tried what you asked me to do.. But still it doesn't store the number.. I m missing something.. Damn..!

    Java Code:
    package w2_2;
    
    import javax.swing.JOptionPane;
    import javax.swing.JTextArea;
    import java.util.Random;
    
    public class w2_2 {
    	
    	public static void main(String args[]){
    		Random dice = new Random();
    		int number;
    		int store[];
    		store = new int[11];
    		
    		for (int counter=1; counter<=10; counter++){
    			number = 2+ dice.nextInt(11);
    			
    			//store[counter]=number;
    			System.out.println(number);
    		}
    		//System.out.println(store[2]);
    		
    	}
    
    }

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default

    it doesn't store the number
    array that is indexed by the sum
    That says what to do. Think about it. You use the sum as the index to the array.
    The array elements are initialized to 0. To add one to an element of an array of ints you'd code: array[i]++;


    Your code uses the loop control variable as the index, not the sum.

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

    Default

    Quote Originally Posted by abby0910 View Post
    Thanks.. I will keep in mind about formatting.. So i tried what you asked me to do.. But still it doesn't store the number..
    Follow each of the steps provided in Norm's first post.

  7. #7
    abby0910 is offline Member
    Join Date
    Jul 2010
    Posts
    10
    Rep Power
    0

    Default

    Great.. I Got the storing array part working now.. My modified code is..

    Java Code:
    package w2_2;
    
    import javax.swing.JOptionPane;
    import javax.swing.JTextArea;
    import java.util.Random;
    
    public class w2_2 {
    	
    	public static void main(String args[]){
    		Random dice = new Random();
    		int Dice1;
    		int Dice2;
    		int number;
    		int store[];
    		store = new int[11];
    		
    		for (int counter=1; counter<=10; counter++){
    			Dice1 = 1+ dice.nextInt(6);
    			Dice2 = 1+ dice.nextInt(6);
    			number = Dice1+Dice2;
    			
    			store[counter] =number;
    			System.out.println(number);
    		}
    		System.out.println("Array at 3rd pt: is " + store[3]);
    		
    	}
    
    }
    So now Would i need a 2d array? Or wld tht b sum kind loop. for the part: Output your result in a table which shows each value (ie. 2 12) and the number of times that value was rolled.

    And by the way.. Thank you guys for your prompt replies..!! Feels good..!

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default

    Did you read my last post about indexing the array? Especially the last line!

  9. #9
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,343
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by abby0910 View Post
    Java Code:
    number = 2+ dice.nextInt(11);
    That does not properly simulate rolling of two dice; e.g. the probability for rolling 12 (six and six) in your simulation is 1/12 but it is 1/36 with two dice. Add two random numbers in the range [1,6] together instead.

    kind regards,

    Jos

    p.s. if you really want to use a single random number for the two dice do this:

    Java Code:
    int r= random.nextInt(36);
    int total= r/6+r%6+2;
    Last edited by JosAH; 07-11-2010 at 11:41 AM.

  10. #10
    abby0910 is offline Member
    Join Date
    Jul 2010
    Posts
    10
    Rep Power
    0

    Default

    @ JosAH.. Yes.. I realized tht.. Tht is y I corrected in last piece of code I pasted where I take 2 dices, each generating a random no. Between 1 to 6.. And then adding the two dices..! Hopefully tht is correct, rite?

  11. #11
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,343
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by abby0910 View Post
    @ JosAH.. Yes.. I realized tht.. Tht is y I corrected in last piece of code I pasted where I take 2 dices, each generating a random no. Between 1 to 6.. And then adding the two dices..! Hopefully tht is correct, rite?
    Yep, your code for generating the sum of two dice in your previous reply is correct (silly me, I didn't see that reply). It's all about probability distributions: i.e. the sum of two U(1, 6) distributed numbers isn't one U(2, 12) distributed number.

    kind regards,

    Jos

  12. #12
    abby0910 is offline Member
    Join Date
    Jul 2010
    Posts
    10
    Rep Power
    0

    Default

    So far i got my code doing what it is supposed to do.. I need help with just 1 last thing.. I broke my head for more then 5 hours for this.. I need to display those 1000 random generated numbers in GUI using JTextArea.. For the most part.. I can make it show the text.. Lemme just paste the code..


    Java Code:
    for(int face =2; face<store.length;face++){
    			System.out.println(face+"\t"+store[face]);
    		}
    				
    		
    		JTextArea area = new JTextArea( );
    		String output = "Face \t Frequency\n";
    		area.setText(output);
    		area.setRows(13);
    		area.setColumns(2);
    		JOptionPane.showMessageDialog( null, area, "Rolling Dice", 
    				JOptionPane.INFORMATION_MESSAGE );
    But how do i add that for loop in the GUI?

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

    Default

    You put the loop in an ActionListener that is added to a JButton so that when the button is pressed, the loop loops, and the end results are set into the JTextArea.

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default

    how do i add that for loop in the GUI
    Does the output from the loop need to be a result of the user asking for it?
    Or if you always want to show the results after they are computed, you wouldn't need a button, just put the loop to generate the contents for the textarea after the results have been saved before you call the JOptionPane.

  15. #15
    abby0910 is offline Member
    Join Date
    Jul 2010
    Posts
    10
    Rep Power
    0

    Default

    @ Fubarable: I dont need the user to do anything. I just have to output the computed results into GUI. Like

    @ Norm: Umm.. That is what i need help for, how do I generate the contents for the textarea, or do i save the results? If u look back at the code, my results are coming out of a for loop. Because I am just starting to learn java, i don't very well know how to JOptionPane to display for loop without displaying each number in separate box.

  16. #16
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default

    Build a String in the loop by concatenating a String for each row to be displayed in the text area. End the String for a line with a newline char: "\n"
    On exiting the loop, put that String into the textarea.

  17. #17
    abby0910 is offline Member
    Join Date
    Jul 2010
    Posts
    10
    Rep Power
    0

    Default

    Tried doing that. But what it does is it just stores the last row,which would make sense..
    Java Code:
    for(int face =2; face<store.length;face++){
    			display = face+"\t"+store[face]+"\n";
    			area.setText(display);
    			}

  18. #18
    abby0910 is offline Member
    Join Date
    Jul 2010
    Posts
    10
    Rep Power
    0

    Default

    nvm.. I got it..!! I had to put area.setText(display); outside the for loop..and also display+=face+"\t"+store[face]+"\n";

  19. #19
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default

    On exiting the loop, put that String into the textarea.

  20. #20
    abby0910 is offline Member
    Join Date
    Jul 2010
    Posts
    10
    Rep Power
    0

    Thumbs up

    Thank you Fubarable and Norm for all your help. My assignment is finally completed. Hence we can mark this thread as Solved. But I dunno how to do it, So if one of the Moderators could..!

    And Thanx once again !

Similar Threads

  1. How do you store a random integer in an array?
    By Unknown in forum New To Java
    Replies: 6
    Last Post: 03-21-2010, 10:40 PM
  2. Replies: 8
    Last Post: 04-19-2009, 05:50 PM
  3. how to Store and recall numbers in java ?
    By Ak-Emm in forum New To Java
    Replies: 16
    Last Post: 09-28-2008, 11:49 AM
  4. random numbers without random class`
    By carlos123 in forum New To Java
    Replies: 4
    Last Post: 01-17-2008, 10:44 PM
  5. generating random numbers in a 5x5 array.
    By acidacid in forum New To Java
    Replies: 3
    Last Post: 08-14-2007, 03:44 AM

Posting Permissions

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