MATLAB: Producing random numbers in Matlab

random number generator; random binary numbers; exact numbers in random binary set

How can I produce random binary numbers with exact number of zeros or ones? for example:
[0 1 0 1 0 1 0 0 1 0] ===> number of ones are 4,
[1 1 0 0 1 0 0 1 0 0] ===> again number of ones are 4,
[1 0 0 1 0 0 0 0 0 1] ===> number of ones are 3, which is not allowed.
I just want exact number of ones in output of random sets.

Best Answer

n=4
a=randperm(2*n)
out=a>n