therrontelford

Filling a matrix with random numbers

Feb 19th, 2018
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.52 KB | None | 0 0
  1. import java.util.Random;
  2. public class CreateArrayWithRandomNumbers {
  3.  
  4.     public static void main(String[] args) {
  5.         double[][] matrix1 = new double[8][6];
  6.        
  7.         for (int i=0; i<matrix1.length; i++){
  8.             for (int j=0; j<matrix1[i].length; j++){
  9.                 matrix1[i][j] = (int)(Math.random()*100);
  10.             }
  11.         }
  12.        
  13.         // OR
  14.         double[][] matrix2 = new double[8][6];
  15.         Random ran = new Random();
  16.         for (int i=0; i<matrix2.length; i++){
  17.             for (int j=0; j<matrix2[i].length; j++){
  18.                 matrix2[i][j] = ran.nextDouble();
  19.             }
  20.         }
  21.     }
  22. }
Advertisement
Add Comment
Please, Sign In to add comment