Random Binary Sequence Generator

조회 수: 119 (최근 30일)
Sam
Sam 2014년 2월 3일
답변: abderrahim rhoulami 2022년 9월 19일
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
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
Sam 2014년 2월 4일
Hi Jos & Roger. Thanks! I didn't realize that the 0.75 was redundant. My idea was to force the random generation to multiply with 0.75 and rounding it but i realize now that it is redundant and forces more 0's..
I agree with Roger's 2nd answer thanks for the clarifications...
Cheers!

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

채택된 답변

Wayne King
Wayne King 2014년 2월 3일
use randi()
x = randi([0 1],10000,1);
  댓글 수: 3
Setsuna Yuuki.
Setsuna Yuuki. 2020년 11월 19일
Muchas gracias :D
Nitin SHrinivas
Nitin SHrinivas 2021년 8월 1일
If we want to generate all unique coloums or rows is there a function to do so? any suggestions

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

추가 답변 (4개)

Jos (10584)
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
Sam 2014년 2월 4일
Hi. Thanks! it works as well but i implemented the earlier one as i needed a 1 line solution since the rest of my code is quite lengthy.
Jos (10584)
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)

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


Ayushi Dadhich
Ayushi Dadhich 2020년 11월 3일
I want a code to generate this output.
  댓글 수: 1
Siddharth Bhalerao
Siddharth Bhalerao 2020년 12월 28일
This space is not for homework/assignments. Ask a specific question. Explain what you have done for the problem you are facing, then ask the question. There are so many experts here, you will definitely get the answers.
Therefore, I will suggest you to update the question and explain the problem clearly.

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


abderrahim rhoulami
abderrahim rhoulami 2022년 9월 19일
A=randi([0],50,10): Whos

abderrahim rhoulami
abderrahim rhoulami 2022년 9월 19일
A=randi([0 1], 50,10) A

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by