We can manipulate matrices in several ways. One of the most interesting is using the library numpy. This library has several methods specialized in matrices.
Well one of the ways you can solve such a question is:
- Randomly draw the values that will be mounted in the matrix;
- Nicely display the mounted array.
As I realized - from your comments - that you want to implement an array without repeated values, I suggest using the method sample library Random.
One of the ways we can implement the code is:
import numpy as np
from random import sample
numeros = sample(range(1, 100 + 1), 9)
print(np.array([numeros[i:i + 3] for i in range(0, len(numeros), 3)]))
Note that numbers is a vector formed by 9 random values not repeated from the range(1, 100 + 1).
The method that ensures no repetition of drawn values is the method sample library Random.
Why will be drawn 9 values?
How do you want a matrix 3 x 3, this implies that the total of elements of the said matrix is 3 x 3 = 9.
So what this code does is basically:
- Assemble a vector with 9 elements drawn and without repetitions;
- Visually organize the distribution of the elements drawn in a matrix 3 x 3.
This way the generated matrix will be displayed more pleasantly.
Your idea with
random
makes total sense. What was the difficulty found?– Woss
Then I created a variable to receive these random values and add them - in the matrix, with . append(). However it error and does not add, I’m having a logic problem, I think.
– ingred almeida
Could [Edit] the question and add this code?
– Woss
Of course, a moment
– ingred almeida
Ingred, if you already have a 3x3 matrix composed of zeros, the
append
will add values in the list, keeping the zeros.– Woss
Oh yes, now he has! However, he is repeating many numbers. Do you know if it is common for Andom? Or has to generate without repetition?
– ingred almeida
Essa está sendo a saída: [ 97 ][ 97 ][ 46 ]
[ 97 ][ 46 ][ 46 ]
[ 97 ][ 46 ][ 53 ]
Ele repete os valores na primeira coluna
– ingred almeida
Ingred Almeida see this code: https://ideone.com/mtkI8U
– Augusto Vasques