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 TypeScript

1 Answer

0 votes
function printAlignedMatrix(matrix: number[][]) {
    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: number, max: number) {
    return Math.floor(Math.random() * (max - min + 1) + min)
}

function generateRandomMatrix(size: number) {
    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:

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

*/

 



answered Nov 3, 2023 by avibootz
...