Generate a 32-bit binary stream

12 vues (au cours des 30 derniers jours)
Steve Rogerson
Steve Rogerson le 21 Fév 2020
Commenté : Steve Rogerson le 26 Fév 2020
I am trying to genrate a binary stream using this function and have searched a lot but came out emty.
The main problem I am facing is mainly the binary addition on every addition and the size of the increasing stream.
The Equation is below:
I tried to use the rand fucntion but it isnt right. I realised the Binary addition was not happening.
The final result should be a float number within [0,1).
pold = [];
pf=0;
for i=1:32
param = round(rand(1,1));
pf = param + pf;
pold(i) = pf*2^(-i);
pold(i);
end

Réponse acceptée

Walter Roberson
Walter Roberson le 22 Fév 2020
sum(randi([0,1],1,32).*2.^-(0:31))
Or
randi([0,2^32-1])/2^32
Or you can use rnd() or related to tell rand to use the shr3cong generator, which has the required results.
Or you could rand() and throw away bits 33 to 52.
  1 commentaire
Steve Rogerson
Steve Rogerson le 26 Fév 2020
Thank you kind Sir!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by