generating zeros and ones with equal probability

조회 수: 7 (최근 30일)
a
a 2013년 6월 12일
댓글: Green N 2015년 6월 11일
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?

채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2013년 6월 12일
rand(1,4)>.5 are rand(1,4)<0.5 the same, because rand(1,n) will generate approximately n/2 numbers <0.5 and n/2 numbers >0.5, mainly when n is big.
  댓글 수: 2
a
a 2013년 6월 12일
편집: a 2013년 6월 12일
i got it,thank you.
you are right.when 'n' is big, only then it is generating approximately equal 0s and 1s...else not.
Green N
Green N 2015년 6월 11일
If I have for example 250*250 matrix with the elements that present probabilities how to turn it into adjacency matrix, i.e. the matrix with 0s and 1s? Thanks for any suggestions

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Walter Roberson
Walter Roberson 2013년 6월 12일
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
a
a 2013년 6월 12일
actually i need equal equal 0s and 1s.
Walter Roberson
Walter Roberson 2013년 6월 12일
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
Roger Stafford 2013년 6월 12일
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
a
a 2013년 6월 12일
편집: a 2013년 6월 12일
thanks for your reply
Walter Roberson
Walter Roberson 2013년 6월 12일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Random Number Generation에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by