Generate Random Sample of 0's and 1's

조회 수: 4 (최근 30일)
Joe
Joe 2014년 1월 16일
댓글: Suneesh 2014년 1월 16일
Hi guys,
how do I generate a random sample of 0's and 1's of size N, such that the probability of of 1 being chosen is p.
I can create a permutation however I can't seem to incorporate probability p into it. Plus, also is there a way I could quickly sum the components of the permutation.
Thanks,
Joe

채택된 답변

Suneesh
Suneesh 2014년 1월 16일
편집: Suneesh 2014년 1월 16일
MATLAB has a function RANDSRC (part of the Comm Sys Tbx) which does this.
out = randsrc(1,N,[0 1; (1-p) p])
See documentation for the function to learn more about the usage.
  댓글 수: 2
Joe
Joe 2014년 1월 16일
thanks this is useful, how about summing the individual components, is there a function?
Suneesh
Suneesh 2014년 1월 16일

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

추가 답변 (1개)

Rajiv Ghosh-Roy
Rajiv Ghosh-Roy 2014년 1월 16일
How about
a = rand(1, N) < p
This creates a random vector of size N, and the < will return 1 if true, 0 if false.
  댓글 수: 4
Rajiv Ghosh-Roy
Rajiv Ghosh-Roy 2014년 1월 16일
Please consult the help for rand. rand(1, N) will generate a 1xN vector of random numbers uniformly distributed in [0, 1]. The probability that a number is in (0, p) is then p.
Suneesh
Suneesh 2014년 1월 16일
편집: Suneesh 2014년 1월 16일
Joe, your problem may be stated as "Generate random numbers that have a Bernoulli distribution with the head(1) probability = p". There are several algorithms for generating random numbers and many start with generating random numbers that are uniformly distributed. This is what RAND does. The comparison " < p" owes to the inverse transform method of generating arbitrarily distributed random numbers from a uniform distribution. However, as far as I remember it should be "<=p". This method is standard in several text books on beginning probability theory. See also:
2. "Inverse Transform Method for simulating continuous random variables." in

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

태그

아직 태그를 입력하지 않았습니다.

Community Treasure Hunt

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

Start Hunting!

Translated by