It has been a very long time since I've used Python.
What I'm looking for: I would like to create a 6-by-6 random matrix where each component is either 1 or 2, so that there are 18 ones and 18 twos.
import numpy import random array = numpy.zeros((6, 6)) while (array == 1).sum() != 18 or (array == 2).sum() != 18: for i in range(0, 6): for j in range(0, 6): array[i][j] = random.randint(1, 2) print array
Sure, this works. But this seems to me to be extremely inefficient, as the code is literally having to insert new matrix values until the number of ones/twos is 18. How can I make this more efficient?