mercredi 1 mars 2017

Generating random numbers in specific binary range

I have a matrix with decimal numbers and I know I can convert them to binary using this code:

m = reshape(dec2bin(typecast(b(:),'uint8'),8).',1,[]);
b_recovered = reshape(typecast(uint8(bin2dec(reshape(m,8,[]).')),'double'),size(b));

Using this code, I think 8 last bits are most significant bits. I want to generate random decimal numbers and replace these elements as 8 last bits won't change after replacing with generated random numbers. I need to have a new number while keeping 8 last bits.

For example:

b=-1.12;
m=1110110001010001101110000001111010000101111010111111000110111111;

be replaced by:

 m=0000110000000000001110000000011010000001100010000010000110111111;

Which is equal to:

b=-1.337678432804527e-04

I know that I can generate random decimal numbers between two numbers but I'm not sure how to solve the above mentioned problem.

mymatrix(1:q,:)= value2 + (value2-value1).*rand(q,size(y_blk,2));




Aucun commentaire:

Enregistrer un commentaire