If you are not too concerned about efficiency, and I suspect that you are not, you could use a while loop.
p = 0.8;
Y(35,1) = false;
ones_count = 0;
i = 1;
while (ones_count < 25)
Y(i) = (rand <= p);
ones_count = ones_count + Y(i);
i = i + 1;
end
Y(i : end) = [];
number_of_rands = length(Y);
Of course, the initial allocation of 35 elements for Y may not be sufficient, but that is unlikely. Even if not sufficient, Matlab will create the new elements for you, albeit inefficiently, when you do Y(i) = ... for i > 35. In any case writing:
Makes your code inefficient, so no point worrying about efficiency.
Best Answer