mask = grid > 0.5
masklength = np.sum(mask)
second_mask = np.zeros((4, 4), dtype=np.bool)
second_mask[mask] = np.random.rand(masklength) < prob
grid[second_mask] = 100
Using multiple levels of boolean index mask in NumPy