user3375975
user3375975

Reputation: 13

How to create a random matrix?

I want create a random matrix like [[[100, 50, 25], [22, 75, 195]]]

My code is

n = 1
r = 2
e = 3

sup = []

for i in range(n):
    sup1 = []
    for c in range(r):
       sup0 = list (random.randint (200, 0, e))
       sup1.append (sup0)
    sup.append (sup1)    

print sup

but python give me error.

Upvotes: 0

Views: 1122

Answers (2)

Aman
Aman

Reputation: 8985

You can use numpy to directly get the random matrix of desired size with values in a given range.

>>> numpy.random.randint(low = 0, high = 200, size=(2, 4))
array([[ 75,  21, 132,  90],
       [112,  11, 104, 114]])

>>> r = 2
>>> n = 1
>>> numpy.random.randint(low = 0, high = 200, size=(r, n))
array([[94],
       [51]])

More details

Upvotes: 0

Hyperboreus
Hyperboreus

Reputation: 32429

This should work (No idea what e does):

sup = [[random.randint(0, 200) for _ in range(r)] for _ in range(n)]

Upvotes: 2

Related Questions