jeudi 12 décembre 2019

generating random list of given numbers with limitations and certain length with python

I want to generate a binary matrix (16*15) of random 1s and 0s with these conditions:

  • no more than 2 consecutive equal numbers in each row.
  • if there are consecutive equal numbers in each row no more than 3 pairs of them are allowed.
  • the quantity of 1s and 0s in each row must be 8 and 7 or 7 and 8 respectively.

I have this code for generating a random list but I don't know how to add the conditions at the random part.

import random
import numpy as np

arr_len = 15
num_ones = 8

pattern = np.zeros(arr_len, dtype=int)
idx = np.random.choice(range(arr_len), num_ones, replace=False)
pattern[idx] = 1
print (pattern)




Aucun commentaire:

Enregistrer un commentaire