Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,870 questions

51,793 answers

573 users

How to create an M x N matrix with random numbers in Java

1 Answer

0 votes
import java.util.Random;
import java.util.Arrays;
    
public class Example {
    private static double[][] createMtrixWithRandomNumbers(int rows, int cols) {
        double[][] randomMatrix = new double[rows][cols];
        Random rand = new Random(); 
        rand.setSeed(System.currentTimeMillis()); 
  
        for (int i = 0; i < rows; i++) {   
            for (int j = 0; j < cols; j++) {
                Integer num = rand.nextInt() % 100; 
                randomMatrix[i][j] = Math.abs(num);
            }
        }
         
        return randomMatrix;
    }
     
    public static void main(String[] args) {
         System.out.println(Arrays.deepToString(createMtrixWithRandomNumbers(4, 5)));
    }
}
 
    
    
/*
run:
     
[[84.0, 92.0, 37.0, 70.0, 10.0], [6.0, 43.0, 46.0, 3.0, 32.0], [3.0, 53.0, 85.0, 41.0, 43.0], [83.0, 11.0, 76.0, 69.0, 77.0]]
 
*/

 



answered May 14, 2024 by avibootz
edited May 14, 2024 by avibootz
...