How to fill random numbers in a 2D array in Java

4 Answers

0 votes
package javaapplication1;

public class JavaApplication1 {

    public static void main(String[] args) {

        int[][] array2D = new int[4][5]; 
     
        for (int i = 0; i < array2D.length; i++ ) 
            for (int j = 0; j < array2D[i].length; j++ ) 
                 array2D[i][j] = (int)(Math.random()*10); // generate doubles
        
        for (int i = 0; i < array2D.length; i++ ) {
            for (int j = 0; j < array2D[i].length; j++ ) {
                System.out.print(array2D[i][j] + " ");
            }
            System.out.println();
        }
    }
}
 
/*

run:

9 5 8 5 1 
1 8 2 8 1 
6 0 6 4 5 
3 3 9 8 1 

*/

 



answered Oct 8, 2016 by avibootz
0 votes
package javaapplication1;

import java.util.Random;

public class JavaApplication1 {

    public static void main(String[] args) {

        int[][] array2D = new int[4][5]; 
     
        Random randomGenerator = new Random();
         
        for (int i = 0; i < array2D.length; i++ ) 
            for (int j = 0; j < array2D[i].length; j++ ) 
                 array2D[i][j] = randomGenerator.nextInt(100); // 0 - 99
        
        for (int i = 0; i < array2D.length; i++ ) {
            for (int j = 0; j < array2D[i].length; j++ ) {
                System.out.print(array2D[i][j] + " ");
            }
            System.out.println();
        }
    }
}
 
/*

run:

85 30 49 87 57 
55 54 23 10 85 
28 52 49 64 53 
15 11 44 37 8 

*/

 



answered Oct 8, 2016 by avibootz
0 votes
package javaapplication1;

import java.util.Random;

public class JavaApplication1 {

    public static void main(String[] args) {

        int[][] array2D = new int[4][5]; 
     
        Random randomGenerator = new Random();
         
        for (int[] array2D1 : array2D) {
            for (int j = 0; j < array2D1.length; j++) {
                array2D1[j] = randomGenerator.nextInt(100); // 0 - 99
            }
        }
        
        for (int[] array2D1 : array2D) {
            for (int j = 0; j < array2D1.length; j++) {
                System.out.print(array2D1[j] + " ");
            }
            System.out.println();
        }
    }
}
 
/*

run:

96 45 38 42 42 
29 88 86 65 49 
71 25 19 67 77 
6 25 45 21 68 

*/

 



answered Oct 8, 2016 by avibootz
0 votes
package javaapplication1;

import java.util.Random;

public class JavaApplication1 {

    public static void main(String[] args) {

        int[][] array2D = new int[4][5]; 
     
        Random randomGenerator = new Random();
         
        for (int[] array2D1 : array2D) {
            for (int j = 0; j < array2D1.length; j++) {
                array2D1[j] = randomGenerator.nextInt(100) + 1; // 1 - 100
            }
        }
        
        for (int[] array2D1 : array2D) {
            for (int j = 0; j < array2D1.length; j++) {
                System.out.print(array2D1[j] + " ");
            }
            System.out.println();
        }
    }
}
 
/*

run:

36 93 68 5 97 
3 60 61 90 8 
69 55 10 54 18 
25 88 100 62 97 

*/

 



answered Oct 8, 2016 by avibootz

Related questions

3 answers 299 views
2 answers 213 views
1 answer 180 views
1 answer 143 views
1 answer 109 views
...