Results 1 to 6 of 6
Thread: Java random numbers in an array!
 09122012, 06:01 AM #1Member
 Join Date
 Dec 2011
 Posts
 28
 Rep Power
 0
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()*(21)+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(""); } } }
 09122012, 06:16 AM #2Member
 Join Date
 Dec 2011
 Posts
 28
 Rep Power
 0
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()*(21+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(""); } } }
 09122012, 07:04 AM #3
Re: Java random numbers in an array!
It is recommended to use appropriate methods of java.util,.Random instead of Math#random()
dbIf you're forever cleaning cobwebs, it's time to get rid of the spiders.
 09122012, 09:44 AM #4
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,131
 Blog Entries
 7
 Rep Power
 24
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,
JosThe only person who got everything done by Friday was Robinson Crusoe.
 09132012, 06:39 AM #5Member
 Join Date
 Dec 2011
 Posts
 28
 Rep Power
 0
 09132012, 08:25 AM #6
Similar Threads

Lottery  NonRepeating Random Numbers from Array
By adize in forum New To JavaReplies: 4Last Post: 01272012, 12:18 PM 
I need help generating random numbers for a fill array
By william979 in forum New To JavaReplies: 15Last Post: 05042011, 12:57 AM 
Array of random numbers ...
By shane1987 in forum New To JavaReplies: 31Last Post: 11142010, 10:33 PM 
Store Random numbers into Array
By abby0910 in forum New To JavaReplies: 19Last Post: 07122010, 12:59 AM 
generating random numbers in a 5x5 array.
By acidacid in forum New To JavaReplies: 3Last Post: 08142007, 03:44 AM
Bookmarks