Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a random matrix in python

Tags:

python

numpy

I am trying to create a random square matrix of nxn random numbers with numpy. Of course I am able to generate enough random numbers but I am having trouble using numpy to create a matrix of variable length. This is as far as I have gotten thus far:

def testMatrix(size):
    a = []
    for i in range(0, size*size):
        a.append(randint(0, 5))

How can I put this list into an array of size x size?

like image 494
J.Doe Avatar asked Dec 31 '25 10:12

J.Doe


1 Answers

Try

np.random.randint(0, 5, size=(s, s))
like image 150
ChootsMagoots Avatar answered Jan 03 '26 00:01

ChootsMagoots



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!