I want to generate a random array of size N which only contains 0 and 1, I want my array to have some ratio between 0 and 1. For example, 90% of the array be 1 and the remainin
If you want an exact 1:9 ratio:
nums = numpy.ones(1000) nums[:100] = 0 numpy.random.shuffle(nums)
If you want independent 10% probabilities:
nums = numpy.random.choice([0, 1], size=1000, p=[.1, .9])
or
nums = (numpy.random.rand(1000) > 0.1).astype(int)