MATLAB: Generate Random Sample of 0’s and 1’s

Hi guys,
how do I generate a random sample of 0's and 1's of size N, such that the probability of of 1 being chosen is p.
I can create a permutation however I can't seem to incorporate probability p into it. Plus, also is there a way I could quickly sum the components of the permutation.
Thanks,
Joe

Best Answer

MATLAB has a function RANDSRC (part of the Comm Sys Tbx) which does this.
out = randsrc(1,N,[0 1; (1-p) p])
See documentation for the function to learn more about the usage.