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 PHP

1 Answer

0 votes
function printMatrix($matrix, $size) {
    for ($i = 0; $i < $size; $i++) {
        for ($j = 0; $j < $size; $j++) {
            printf("%4d", $matrix[$i][$j]);
        }
        echo "\n";
    }
}

function generateRandomMatrix($size)  {
    $matrix = array_fill(0, $size, array_fill(0, $size, 0));
    
    for ($i = 0; $i < $size; $i++) {
        for ($j = 0; $j < $size; $j++) {
            $matrix[$i][$j] = rand(1, 100);
        }
    }
    return $matrix;
}
        
$matrix = generateRandomMatrix(20);

printMatrix($matrix, 20);



/*
run:

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

 



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