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 comentarios

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)
Jos (10584) el 3 de Feb. de 2014
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
Roger Stafford el 3 de Feb. de 2014
Editada: Roger Stafford el 3 de Feb. de 2014
I assumed that was what was wanted. Otherwise round(rand(1,10000)) is more efficient.
Sam
Sam el 4 de Feb. de 2014
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!

Iniciar sesión para comentar.

 Respuesta aceptada

Wayne King
Wayne King el 3 de Feb. de 2014
use randi()
x = randi([0 1],10000,1);

3 comentarios

monika&bouta
monika&bouta el 8 de Mayo de 2016
hi, we did the same command but w need to put in polar form. any ideas !!?
Setsuna Yuuki.
Setsuna Yuuki. el 19 de Nov. de 2020
Muchas gracias :D
Nitin SHrinivas
Nitin SHrinivas el 1 de Ag. de 2021
If we want to generate all unique coloums or rows is there a function to do so? any suggestions

Iniciar sesión para comentar.

Más respuestas (2)

Jos (10584)
Jos (10584) el 3 de Feb. de 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 comentarios

Sam
Sam el 4 de Feb. de 2014
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) el 6 de Feb. de 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)

Iniciar sesión para comentar.

Categorías

Más información sobre Programming en Centro de ayuda y File Exchange.

Productos

Etiquetas

Preguntada:

Sam
el 3 de Feb. de 2014

Respondida:

el 19 de Sept. de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by