How to initialize N random vectors, where N is provided by the user?

Asked

Viewed 37 times

0

import random

x = int(input("Digite o tamando do cromossoma:  "))
n = int(input("Digite a quantidade de vetores:  "))
saida = []
for _ in range(x):
    saida.append(random.randint(0, 1))
print(saida)
  • If one of the answers below solved your problem and there was no doubt left, choose the one you liked the most and mark it as correct/accepted by clicking on the " " that is next to it, which also marks your question as solved. If you still have any questions or would like further clarification, feel free to comment.

1 answer

1

One solution to this type of situation is to use comprehensilist on:

import random

x = int(input("Digite o tamando do cromossoma:  "))
n = int(input("Digite a quantidade de vetores:  "))
saida = []
for _ in range(x):
    saida.append([random.randint(0,1) for i in range(n)])

for vector in saida:
    print(vector)

Returns n dimension vectors x each one.

Browser other questions tagged

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