MATLAB: Sampling to obtain number of successes within an interval

sample from an interval

I want to create an n row by 40 column array of zeroes and 1's representing values of 20 bi-allelic genetic loci, with the 1's randomly assigned to the 40 possible locations (columns).. But I need to have the total number of 1's be no greater than min and no greater than max. For example, suppose I want the mean number of 1's to be 12 (30% of the 40 total alleles) but I want the number to be a least 10 and at most 14. Simply darwing a uniform random on [0,1] wiht probability of success = 0.3 will not work, as individual row may have fewer than min 1's or more than max 1's.

Best Answer

Try this
n = 100;
n_cols = 40;
min_val = 10;
max_val = 14;
mean_val = (min_val+max_val)/2;
num_ones = randi([min_val max_val], n, 1);
M = zeros(n, n_cols);
for i=1:n
idx = randperm(40, num_ones(i));
M(i, idx) = 1;
end
test that it produces the required distribution of 1s
>> row_sum = sum(M,2); % sum of all rows
>> min(row_sum)
ans =
10
>> max(row_sum)
ans =
14
>> mean(row_sum)
ans =
12.0100