vendredi 26 février 2021

How to make a random matrix without numpy

I came across this problem: create a random matrix without numpy.

I searched a bit for the term, but I didn't get it. Do u have to re-seed everytime I'm searching for a random number? My solution to this was:

import random

def matrix_random_number(n_filas, n_columnas, num_decimals=2):
    blank = [0]
    row = blank*n_filas
    array = [row]*n_columnas
    
    for j in range(n_columnas):
        for i in range(n_filas):
            array[j][i] = random.randint(0,100*10**num_decimals)/10**num_decimals
    return array

But my output was:

[[80.91, 47.46, 15.86, 77.16, 92.47, 54.92, 2.76, 97.42, 14.99, 15.97],
 [80.91, 47.46, 15.86, 77.16, 92.47, 54.92, 2.76, 97.42, 14.99, 15.97],
 [80.91, 47.46, 15.86, 77.16, 92.47, 54.92, 2.76, 97.42, 14.99, 15.97],
 [80.91, 47.46, 15.86, 77.16, 92.47, 54.92, 2.76, 97.42, 14.99, 15.97],
 [80.91, 47.46, 15.86, 77.16, 92.47, 54.92, 2.76, 97.42, 14.99, 15.97],
 [80.91, 47.46, 15.86, 77.16, 92.47, 54.92, 2.76, 97.42, 14.99, 15.97],
 [80.91, 47.46, 15.86, 77.16, 92.47, 54.92, 2.76, 97.42, 14.99, 15.97]]

So that this is clearly not random. How to improve? Why is this bad code? Thanks in advance




Aucun commentaire:

Enregistrer un commentaire