FILL MATRIX WITH PYTHON RANDOM VALUES

Asked

Viewed 266 times

-2

I need to create an array of random values, I was thinking of using Random but I’m having difficulty adding this data to the matrix; I’ve already defined that is a 3x3 matrix. Could someone help me, please?

I tried so and made another attempt with numpy too, but without success:

import random

matrix = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]

for l in range(0,3):
    num = random.randint(0, 100)
    matrix[l].append(num)
    for c in range(0, 3):
        matrix[c].append(num)

for l in range(0, 3):
    for c in range(0, 3):
        print(f'[{matrix[l][c]:^5}]', end='')
    print()
  • 1

    Your idea with random makes total sense. What was the difficulty found?

  • 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.

  • 1

    Could [Edit] the question and add this code?

  • Of course, a moment

  • Ingred, if you already have a 3x3 matrix composed of zeros, the append will add values in the list, keeping the zeros.

  • 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?

  • Essa está sendo a saída: [ 97 ][ 97 ][ 46 ]
[ 97 ][ 46 ][ 46 ]
[ 97 ][ 46 ][ 53 ]
Ele repete os valores na primeira coluna

  • Ingred Almeida see this code: https://ideone.com/mtkI8U

Show 3 more comments

2 answers

1


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:

  1. Randomly draw the values that will be mounted in the matrix;
  2. 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:

  1. Assemble a vector with 9 elements drawn and without repetitions;
  2. Visually organize the distribution of the elements drawn in a matrix 3 x 3.

This way the generated matrix will be displayed more pleasantly.

  • 1

    Thank you so much! I will test here with numpy

  • 1

    Where is the command not to repeat? I could not locate and understand

  • @ingred Almeida, The command that restricts repetition is the method sample. This method draws the values without repeats in the specified range. Otherwise, if you do not have the library installed, you will have to install.

  • 1

    I have installed yes, thank you!

0

The list valores has a list of the random numbers to be generated. Random generates values in the range 0 to the number number in valores, and when the Dice value is generated it is removed from the list, to prevent it from repeating again.

import random
import numpy as np

matrix = []
valores =  list(np.arange(100))

for l in range(0,3):
    subMatriz= []
    
    for c in range(0, 3):
      i = random.randint(0, len(valores)-1)
      num = valores[i]
      valores.pop(i)
      subMatriz.append(num)
    matrix.append(subMatriz)

for l in range(0, 3):
    for c in range(0, 3):
        print(f'[{matrix[l][c]:^5}]', end='')
    print()

Browser other questions tagged

You are not signed in. Login or sign up in order to post.