Results 1 to 6 of 6
  1. #1
    dharav is offline Member
    Join Date
    Dec 2011
    Posts
    28
    Rep Power
    0

    Question Java random numbers in an array!

    I have to initialized a 6 row by 6 column array. And am trying to add ten 5's in the array. And then print it but it wont work. I tried for 2 hours cant help it, can anyone please help me!
    Thank you forum!
    Java Code:
    package HW7_5_3;
    import java.util.Scanner;
    import java.lang.Math;
    public class Task5review {
    
    	public static void main(String[] args) {
    
    		int array1[][] = new int [6][6];
    		int num;
    		int count=0;
    		if(count<10)
    		{
    			for(int i=0;i<6;i++)
    			{
    				for(int k=0;k<6;k++)
    				{
    					num=(int)(Math.random()*(2-1)+2);
    					if (num==1&&count<10)
    					{
    						array1[i][k]=5;
    						count++;
    					}
    					if (num==2)
    					{
    						array1[i][k]=0;
    					}
    				}
    			}
    		}
    		for(int i=0;i<6;i++)
    		{
    			for(int k=0;k<6;k++)
    			{
    				System.out.print(array1[i][k]+" ");
    			}
    		System.out.println("");
    		}
    	}
    }

  2. #2
    dharav is offline Member
    Join Date
    Dec 2011
    Posts
    28
    Rep Power
    0

    Default Re: Java random numbers in an array!

    Sorry nvm i got it thenk you for who ever looked at it this is the solution
    Java Code:
    package HW7_5_3;
    import java.util.Scanner;
    import java.lang.Math;
    public class Task5review {
    
    	public static void main(String[] args) {
    
    		int array1[][] = new int [6][6];
    		int num;
    		int count=0;
    		if(count<10)
    		{
    			for(int i=0;i<6;i++)
    			{
    				for(int k=0;k<6;k++)
    				{
    					num=(int)(Math.random()*(2-1+1)+1);
    					if (num==1&&count<10)
    					{
    						array1[i][k]=5;
    						count++;
    					}
    					if (num==2)
    					{
    						array1[i][k]=0;
    					}
    					
    				}
    			}
    		}
    	
    			for(int i=0;i<6;i++)
    			{
    				for(int k=0;k<6;k++)
    				{
    					System.out.print(array1[i][k]+" ");
    				}
    			System.out.println("");
    			}
    		
    	}
    }

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default Re: Java random numbers in an array!

    It is recommended to use appropriate methods of java.util,.Random instead of Math#random()

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,679
    Blog Entries
    7
    Rep Power
    21

    Default Re: Java random numbers in an array!

    That algorithm doesn't always produce ten '5's at random positions in your two dimensional array.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    dharav is offline Member
    Join Date
    Dec 2011
    Posts
    28
    Rep Power
    0

    Default Re: Java random numbers in an array!

    Quote Originally Posted by JosAH View Post
    That algorithm doesn't always produce ten '5's at random positions in your two dimensional array.

    kind regards,

    Jos
    What do you mean please quote me the line so i can check.
    Thx

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default Re: Java random numbers in an array!

    Test
    Debug
    Rinse
    Repeat

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 4
    Last Post: 01-27-2012, 12:18 PM
  2. I need help generating random numbers for a fill array
    By william979 in forum New To Java
    Replies: 15
    Last Post: 05-04-2011, 01:57 AM
  3. Array of random numbers ...
    By shane1987 in forum New To Java
    Replies: 31
    Last Post: 11-14-2010, 10:33 PM
  4. Store Random numbers into Array
    By abby0910 in forum New To Java
    Replies: 19
    Last Post: 07-12-2010, 01:59 AM
  5. generating random numbers in a 5x5 array.
    By acidacid in forum New To Java
    Replies: 3
    Last Post: 08-14-2007, 04: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
  •