Random Binary Sequence Generator
222 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Sam
am 3 Feb. 2014
Beantwortet: abderrahim rhoulami
am 19 Sep. 2022
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 Kommentare
Roger Stafford
am 3 Feb. 2014
Bearbeitet: Roger Stafford
am 3 Feb. 2014
I assumed that was what was wanted. Otherwise round(rand(1,10000)) is more efficient.
Akzeptierte Antwort
Wayne King
am 3 Feb. 2014
use randi()
x = randi([0 1],10000,1);
3 Kommentare
Nitin SHrinivas
am 1 Aug. 2021
If we want to generate all unique coloums or rows is there a function to do so? any suggestions
Weitere Antworten (4)
Jos (10584)
am 3 Feb. 2014
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 Kommentare
Jos (10584)
am 6 Feb. 2014
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
am 3 Nov. 2020
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/401549/image.png)
1 Kommentar
Siddharth Bhalerao
am 28 Dez. 2020
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.
Siehe auch
Kategorien
Mehr zu Resizing and Reshaping Matrices finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!