generating zeros and ones with equal probability
Show older comments
to generate 0s and 1s with equal prob. (probability=.5),
i used
>> rand(1,4) > .5
and if i use this >> rand (1,4) < .5
what is the difference between these two commands?
Accepted Answer
More Answers (2)
Walter Roberson
on 12 Jun 2013
If you have four items each with two different possible states of equal probability, then you have 16 possible outcomes:
0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111
Of these 16, the patterns that have equal numbers of 0's and 1's, are:
0011 0101 0110 1001 1010 1100
which is 6 out of 16, so a 3/8 probability.
Thus, if you choose 4 items at random with equal weight, there will be a 5/8 probability that the result will not have equal numbers of 0's and 1's. Therefore, forcing there to be equal number of 0's and 1's would require that the probabilities of the individual items not be independent. In some cases, once the first two values were generated, the remaining two items would be completely forced; in other cases, generating one more bit would force the value of the last bit.
Do you need your probabilities to be independent, or do you need there to be equal numbers of 0's and 1's? The two cases are mutually exclusive.
2 Comments
a
on 12 Jun 2013
Walter Roberson
on 12 Jun 2013
m = [0 0 1 1; 0 1 0 1; 0 1 1 0; 1 0 0 1; 1 0 1 0; 1 1 0 0];
q = randi(size(m,1));
result = m(q,:);
Roger Stafford
on 12 Jun 2013
0 votes
The difference is miniscule. It is the probability that the 'rand' function will happen to generate an exact 1/2. I would judge that the odds against this happening are about one out of ten thousand million million. In other words for all practical purposes you can use either expression.
2 Comments
Walter Roberson
on 12 Jun 2013
More exactly, 1 in (2^53-2) which is 1 in 9007199254740990, which is "one out of nine thousand million million". Not that that makes any difference for practical purposes.
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!