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 20x20 matrix with random numbers between 1 and 100 in Node.js

1 Answer

0 votes
function printAlignedMatrix(matrix) {
    const columnWidths = matrix[0].map((_, colIndex) =>
        Math.max(...matrix.map(row => String(row[colIndex]).length)
    ));
 
    matrix.forEach(row => {
        let rowStr = '';
        row.forEach((cell, colIndex) => {
            const formattedCell = String(cell).padStart(columnWidths[colIndex]);
            rowStr += formattedCell + '  '; 
        });
     
        console.log(rowStr);
    });
}

function generateRandomInteger(min, max) {
    return Math.floor(Math.random() * (max - min + 1) + min)
}

function generateRandomMatrix(size) {
    let matrix = Array(size).fill(0).map(()=>new Array(size).fill(0));
    
    for (let i = 0; i < size; i++) {
        for (let j = 0; j < size; j++) {
            matrix[i][j] = generateRandomInteger(1, 100);
        }
    }
    return matrix;
}

const matrix = generateRandomMatrix(20);

printAlignedMatrix(matrix);





/*
run:

38  69  20    7  68  12  16  58  23  68   9   8  64  81  96  53  19  89  89  28  
47  90  90    5  96  73  50  62  60  25  53  84  80  59  45  39  78  17  25  30  
 8   9  13   18  46  94  88   7  52  38  74  76  50  91  62  50  10  30  64  55  
87  96  87   71  93  12   5   2  90  52  95  82  77  34   9  46  91  74  56  94  
17  39  33   72  47  15  24  67  69  97  89   3  80  26  22  61  78  14  21  33  
93  93  39   90  34  71  74  33  98  54  11  78  17  53  38  89  35  53  56  80  
12  32  86   75  79  15   7  36  77  62  64  91  32  25   7   8  84  76   1  91  
40   9  75  100  44  33  22   8  84  95  19  50  81  43  76  11  91  62  97   6  
22  35  93   99  95  73  99  35  77  57  49  76  64  80  82  69  30  73  50  43  
63  67  10   89  35  47  61  60  10  95  59  18  77  11   6  55  85  25  17  77  
 5  90  47   72  53  18  94  96  23  79  27  95   4  30  29  86   2  13  65  31  
70  94  31   58  75  60  85  70  73  23  62  97  87  45  26  21   6  86  98  97  
76   6  68    5  48  88  53  13  47  53  37  14  49  12  76  11  31  59  19   5  
86  91  90   79  69  35  28  77  63  13  25  62  29  90  36  98  44  10  65  38  
72   9  17   41  81  73  29  82  43  34  51  67   1   5   5  94  35  29  86  71  
95  61  68   43  48  81  97  35   4  47  72  80  29  61  24  34  66   4  76  22  
74  85  99   28  94  42  57   7  41  73  98  14  59  76  45  29  75  31  78  46  
30   6  33   80  81  66  63  70  76  89  20  76   7  82  45  36  50  20  50  12  
37  34  11   87   3  54  57  53  34  17  90  71  70  24  41  51   4  82  48  29  
72  41  98   96  57  66  60  16  11  13  20  44  62  93  76  99  72  20  25  97  

*/

 



answered Nov 3, 2023 by avibootz
...