Generate a 32-bit binary stream
3 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
Walter Roberson
2020-2-22
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.
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!