generating zeros and ones with equal probability

5 ビュー (過去 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 Center および File ExchangeRandom Number Generation についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by