Generate a 32-bit binary stream

3 views (last 30 days)
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

Accepted Answer

Walter Roberson
Walter Roberson on 22 Feb 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.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!