Random Binary Sequence Generator
이전 댓글 표시
I need to generate a Random Binary Sequence of 1x10000 size. Purely Random 1's and 0's.
I had implemented it as follows and it does the job with no complaints.
rand_bin = round(0.75*rand(1,10000));
However, are there any Efficient Implementations for this, probably a specific function which was built for this or something?
Thanks.
댓글 수: 4
Roger Stafford
2014년 2월 3일
This gives you 1's with probability 1/3 and 0's with probability 2/3. You could also do
ceil(rand(1,10000)-2/3)
or
floor(rand(1,10000)+1/3)
for the same probability, but I doubt if these are any more efficient. I doubt if you can improve on these in any significant way.
Jos (10584)
2014년 2월 3일
Why do you have the factor 0.75 in there? This will give more zeros than ones in the output. Are you sure your need that?
Roger Stafford
2014년 2월 3일
편집: Roger Stafford
2014년 2월 3일
I assumed that was what was wanted. Otherwise round(rand(1,10000)) is more efficient.
Sam
2014년 2월 4일
채택된 답변
추가 답변 (2개)
Jos (10584)
2014년 2월 3일
Here are two suggestions:
% with 0 <= P <=1
RBS = rand(1,N) < P
% will give roughly a proportion of P ones among N values
% exactly M ones among N values
RBS = false(1,N) ;
RBS(1:M) = true ;
RBS = RBS(randperm(numel(RBS)
Note: I prefer to store the output as logical arrays (true/false = 1/0) that occupy less memory than double arrays.
댓글 수: 2
Sam
2014년 2월 4일
Jos (10584)
2014년 2월 6일
That's a poor reason to use a one-lined solution ;-)
Long but efficient and readable code is much preferred over short and efficient but unreadable code.
(btw rand(1,N)<P is both short and readable)
abderrahim rhoulami
2022년 9월 19일
0 개 추천
A=randi([0],50,10): Whos
카테고리
도움말 센터 및 File Exchange에서 Programming에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!