From what I understand, you want to implement an array in which all your values are missing repeated.
Note that when we are working with matrices we have to make clear the number of rows and columns.
Well, to solve this issue we can use the following code:
import numpy as np
from random import sample
m = int(input('Digite o número de linhas: '))
n = int(input('Digite o número de colunas: '))
v = (m * n)
# Montando uma matriz qualquer.
if v <= 170:
numeros = sample(range(1, 171), v)
print(np.array([numeros[i:i+n] for i in range(0, len(numeros), n)]))
else:
print('Erro! Quantidade de sorteios maiores que o range!')
When we executed this code we received the first message: Digite o número de linhas:
. At this point we must enter the number of lines of our matrix and press enter
. Then we received the second message: Digite o número de colunas:
. At this point we must enter the number of columns and press enter
.
Note that the block if will control the amount of matrix elements. If this amount is equal to or equal to to the upper limit of the range, the array will be mounted and displayed. Otherwise, we will receive an error message that says: Erro! Quantidade de sorteios maiores que o range!
. This error tells us that we want to generate an array with a larger amount of elements than the supported range.
If the entered values satisfy the arbitrary range, the code will assemble a list named after numbers, containing (m * n) drawn elements of range(1, 171).
Observation 1: I used the range(1, 171), because we know that the range in python always goes from 1 to n - 1. So, to draw values within the closed range [1,170], you must set the range to range(1, 171).
Observation 2: When I point out the number of lines - m - and the number of columns - n - the total number of values drawn will be (m * n).
After the draw the values will be organized and displayed
Let’s test the code?
Create a matrix that has 4 lines and 4 columns, in which each element is randomly drawn from the closed interval [1, 170].
To solve this example, we must execute the code, enter the value 4 - for the number of lines - and press enter
, enter the value 4 - for the number of columns - and press enter
.
From now on the code will assemble and display an order matrix 4 x 4 - with 16 values - with values drawn unrepeated.
I knew how to do it:
tabela = np.random.random_integers(170, size=(4,4))
but I am taking notice of depreciation in this functionrandom_integers()
– Augusto Vasques
and if it is for values like 10 starting and 170 ending? put in appearance?
– 형사씨
It is not good to go this way because the function has been depreciated and soon will stop working. I left the comment because I also want to know the answer.
– Augusto Vasques
OK, I’ll let you
– 형사씨