Randint does not generate random numbers the second time I use it

0

My randint when used the second time it does not generate new random numbers, it stays with the same numbers generated. I know in C when this happens I can use the srand (time (NULL)) , but in Python I'm not finding anything like it

Show 3 different arrays

from random import randint

matriz = [[], [], []]


def gerar_matriz ():
    for c in range(len(matriz)):
        for i in range(0, 3):

            s = randint(0, 9)
            matriz[c].append(s)    # adiciona os números a matriz


for c in range(3):

    gerar_matriz()

    for c in range(len(matriz)):
        for i in range(0, 3):
            print(matriz[c][i], end=' ')
        print(' ')

    print ('\n')
    
asked by anonymous 28.02.2018 / 20:07

1 answer

0

The problem is that you are not creating new arrays on every call to gerar_matriz() - what you do is add new numbers to the end of each existing row.

And at the time of printing the lines, you used a fixed size of 3, so you always see the first three elements of the line. Just change your internal printout to see what's happening:

    ...
    for c in range(len(matriz)):
        for i in range(0, len(matriz[0])):  # alterei esta linha
            print(matriz[c][i], end=' ')
        print(' ')
That's why good programming practices often ask us to do everything within functions - your code that creates a new array and calls the array generation is out of function and can not be executed in order to isolate the array , which is a global variable.

If you do this, it will work:

from random import randint

def gerar_matriz ():
    matriz = [[], [], []]

    for c in range(len(matriz)):
        for i in range(0, 3):

            s = randint(0, 9)
            matriz[c].append(s)    # adiciona os números a matriz
    return matriz

    def imprime():

        for c in range(3):

            matriz = gerar_matriz()

            for c in range(len(matriz)):
                for i in range(0, 3):
                    print(matriz[c][i], end=' ')
                print(' ')
        print ('\n')

    imprime()

All I did there was to stop depending on the "array" as a global variable - it is created inside the function generate_matrix and returned to who called the function. (turning the print code into a function is not essential, but prevents the name "matrix" from being globally associated again, and other functions to see only the last generated matrix).

Another thing is that in Python rarely, rarely even, do you need to use for with range . In general we are interested in the elements of a sequence, not in the indexes of the elements, and then look for the elements.

In the case of the array, we can make a for to get each line, and within a for to get each element:

def imprime():

    for c in range(3):  # aqui sim, queremos repetir algo 3 vezes!
        matriz = gerar_matriz()

        for linha in matriz:
            for elemento in linha:
                print(elemento, end=' ')
            print()
    print ('\n')
    
01.03.2018 / 13:28