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

Asked

Viewed 684 times

0

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

Show 3 separate matrices

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')
  • 1

    randint in its normal use generates random numbers yes. Please post the code of how you are doing. (Using the value of time as Rand Seed would work, but I think you’re missing something more fundamental)

  • I posted the code

1 answer

2

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

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 print out to see what’s going on:

    ...
    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 in general ask us to do everything within functions - your code that creates a new matrix and calls the matrix generation is out of functions and cannot be executed in a way to isolate the matrix, which is a global variable.

If you do so, 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()

Only what I did there was to stop depending on the "matrix" as a global variable - it is created within the function gerar_matrix and returned to who called the function. (transforming the print code into a function is not essential, but avoids that the name "matrix" is again globally associated, and that other eventualmetne functions would see only the last generated matrix).

Another thing is that in Python rarely, even rarely, 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, to then search for the elements.

In the case of the matrix, we can make a for to get each line, and inside a for to obtain 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')
  • Thank you so much for your help

Browser other questions tagged

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