generating zeros and ones with equal probability

7 次查看(过去 30 天)
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 个评论
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 个评论
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