user3275165
user3275165

Reputation: 11

initiante matrixes with ranged random numbers in python

I am looking for a way for initializing a N*N matrix with numbers in range of [-1000,1000] in python, with numpy or any thing else? I have tried:

>>> import numpy as NP

>>> a1 = NP.matrix("4 3 5; 6 7 8; 1 3 13; 7 21 9")
>> a1
matrix([[ 4,  3,  5],
    [ 6,  7,  8],
    [ 1,  3, 13],
    [ 7, 21,  9]])

 >>> a2 = NP.matrix("7 8 15; 5 3 11; 7 4 9; 6 15 4")
>>> a2
matrix([[ 7,  8, 15],
    [ 5,  3, 11],
    [ 7,  4,  9],
    [ 6, 15,  4]])

It is my first project with python and numpy.

Upvotes: 1

Views: 55

Answers (1)

Hannes Ovrén
Hannes Ovrén

Reputation: 21851

Since you have repeating numbers, I assume you don't want all numbers in the range, but just that the chosen numbers are drawn from that range.

In that case, using numpy.random.randint might be what you want.

A = numpy.random.randint(-1000,1000,size=(N,N))

Upvotes: 1

Related Questions