I need to generate a binary array of size N with K numbers 1 and (n-k) numbers 0 on it,does not matter in which position. The amount of K numbers 1s belongs to an interval Min <= K <= Max , and Max <= N .
Since these values is represented by a binary variable, the probability of array position has 0 is P(x=0) = 0.5 and it has 1 is P(x=1) = 1-P(x=0) = 0.5.
Let's suppose an array size of N=100 must have K numbers 1s on it according 20 <= K <= 30 ,i.e. Min=20 and Max=30.
My first idea to use a probability function to generate the values for this array was developed according to the following considerations, which I'm not sure if it is correct :
(1) for the MIN constraint K>=20, since the probability of 1s is P(x=1)=0.5 and it is required that at least 20 positions been filled with 1s then , at least 20/100 =20% , (Min/N) positions must have 1s, However, for a random binary variable P(X=1) = 0.5 , so how to combine this with the constraint of 20% must be 1 ?
(2) for the MAX constraint K<=30, it means that the (100 - 30) remaining positions must be 0, i.e. 70/100 = 70%, (1 - Max/N) positions must have zero. However, for a random binary variable P(X=0)=0.5, how to combine this with the constraint of 70% must be 0 ?
(3) the interval 20 <= K <= 30 , can be either 0 or 1, i.e. (30 - 10)/100= 20% , (Max - Min)/N positions can be filled 0 or 1.
After found the probability of 1s "prob_one", I'd like to write my code using a "rand" function (which returns a random number between 0 and 1) to fill each position of the binary array like this :
"if rand <= prob_one then myarray[position] := 1 else myarray[position] = 0" .
My Questions :
Q1) Do considerations (1) , (2) and (3) make sense ?
Q2) Is there any probability distribution function that can solve this problem and give a direct answer like P(X=1) = prob_one and P(X=0)= prob_zero ?
I appreciate solutions, explanations, examples and material references on how to solve this.