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,884 questions

51,810 answers

573 users

How to create a 20x20 matrix with random numbers between 1 and 100 in Python

1 Answer

0 votes
import random
 
def CreateMatrix(rows, cols, start, end):
    # Create a rowsxcols matrix with random numbers between 1 and 100
    matrix = [[random.randint(start, end) for _ in range(cols)] for _ in range(rows)]
     
    return matrix
  
 
matrix = CreateMatrix(20, 20, 1, 100)
 
for row in matrix:
    for val in row:
        print('{:4}'.format(val), end="")
    print()


 

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

 



answered Oct 30, 2023 by avibootz
...