def random_bool(shape, p=0.5):
    n = np.prod(shape)
x = np.fromstring(np.random.bytes(n), np.uint8, n)
return (x < 255 * p).reshape(shape)
Memory-efficient way to generate a large numpy array containing random boolean values