I have generated a bar graph that counts the number of 1's in each bit of a 16-digit binary string:
I would like to generate 300 binary vectors of 16 bits that roughly follows the above distribution.
Initially, my code depended on a probability array gengen
, which counts the number of times 1 appears in each bit. I generated a random matrix of 300x16 values, compared each bit of value to the probability and assigned it to 1 and 0. This is similar to the weighted coin approach.
However, the distribution I received was nearly uniform.
gengen = [30, 28, 30, 30, 30, 26, 28, 28, 29, 23, 17, 8, 10, 12, 7, 6]
len = 16; % string length
% probability for 1 in each bit
prob_gengen = gengen./30;
% generate 100 random strings
moreStrings = rand(300,len);
samplemore = []
for i = 1:300
for k = 1:16
disp(i)
disp(k)
if (prob_gengen(k) > moreStrings(i,k))
samplemore(i,k) = 1;
else
samplemore(i,k) = 0;
end
end
end
G = reshape(samplemore,[16,300])
And this code plots the final distribution:
colormap('default')
subplot(3,1,1)
bar(sum(G,2)); % summing along rows using DIM = 2
xlabel('Frequency Bin ');
title('Generator (16b) Generated');
What can I do to have a distribution similar to the first bar chart? The code itself is in MATLAB, but I think a Python implementation can work as well.