dimanche 19 juin 2022

Generating 11x6 matrix of randomly placed 0 and 1

I want to generate 11x6 matrix that have randomly placed 0 and 1. I want every row to be symmetric. It is represented by array of 6 arrays of length 11. I used the randint() function from the random module but it generates 6 copies of the same row. Here's my code:

import random

def pattern_generator():
    row = [None] * 11
    pattern = [row] * 6
    for i in range(len(pattern)):
        for j in range(len(row)):
            x = random.randint(0, 1)
            pattern[i][j] = x
            pattern[i][10 - j] = pattern[i][j]
    return pattern

And example of the output:

[[1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1], [1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1], [1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1], [1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1], [1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1], [1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1]]

Can anyone give me any tip what I'm lacking in this function? Thanks in advance




Aucun commentaire:

Enregistrer un commentaire