MATLAB: Random number generator

random number generator

hi…
Can i know how to create a vector with specific number of 1 value? Example i have 5 x 1 matrix, then i want to assign value 1 randomly, but i only want 3 numbers or less of 1's in that matrix..
such as: 1 0 1 1 0
or 0 1 1 0 0
Thanks

Best Answer

Yes you can do it for non repeating randoms. One possible way is which chandra suggested above. But if you want to avoid loops then it is simple. I will generate population of 10 x 15 and. and randomly on each colum 5 values will be zero, other then 1.
population =ones(10,15); % your population
myrand=randperm(10); % random indexes
population(myrand(1:5),:) = 0; % out 10, each 5 fill be replace. This is total random.