 <p>Perhaps this is something you are looking for:</p> <pre><code>from numpy.random import randint n = 50 R = randint(0,2,n) def get_number_of_zeros(x): return sum(0 == ele for ele in x) while(len(R) &gt; 0): number_of_zeros = get_number_of_zeros(R) print 'number of zeros is {}'.format(number_of_zeros) R = randint(0, 2, number_of_zeros) </code></pre> <p>Result:</p> <pre><code>number of zeros is 25 number of zeros is 11 number of zeros is 7 number of zeros is 4 number of zeros is 1 number of zeros is 1 number of zeros is 1 number of zeros is 0 </code></pre>