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 a 10x10 matrix with random numbers between 1 and 100 in Java

1 Answer

0 votes
import java.util.Random;
 
public class MyClass {
     
    public static void printMatrix(int[][] matrix, int size) {
        for (int i = 0; i < size; i++) {  
            for (int j = 0; j < size; j++) {
                System.out.printf("%4d", matrix[i][j]);
            } 
            System.out.println();
        }
    }
 
    public static int generateRandomInteger(int min, int max) {
        return new Random().nextInt(max - min + 1) + min;
    }
     
    private static int[][] generateRandomInteger(int size) {
        int[][] matrix = new int[size][size];
     
        for (int i = 0; i < size; i++) {     
            for (int j = 0; j < size; j++) {
                matrix[i][j] = generateRandomInteger(1, 100);
            }
        }
     
        return matrix;
    }
     
    public static void main(String args[]) {
        int[][] matrix = generateRandomInteger(10);
       
        printMatrix(matrix, 10);
    }
}
 
 
 
 
/*
run:
 
  95  27  40  48  61  53  46  15  99   3
  16  82  62  78  98   5  29  43  47  77
  37  67  63   6  11  98  94  74  47  53
  67  46  11  98  51  23  52  88  85  47
  80  51  67  30  56  91  53  58  84  22
   1  75  90  49  89  58  88  24  57  70
   5  84  46  97  76  67  68  93  24  87
  84  41  83  19  97  14  22   7  47  16
  96  39  96  70  86  44 100   4  13  98
  33  28  56  24   1  91  32  24  23  64
 
*/

 



answered Nov 1, 2023 by avibootz
edited Nov 1, 2023 by avibootz
...