matlab - how to generate bit sequence randomized over 2 bits -
i have network 4 different types of nodes a, b, c , d represented 00, 01, 10 , 11
respectively. want create random sequence of nodes, e.g. a b d c d c b c d (00 01 11 10 00 11 10 01 10 11), such that: no_of_type nodes > no_of_type b nodes > no_of_type c nodes. how can generate such bit sequence using matlab.
use randperm
obtain random permuation of ordered sequence of as, bs, cs , ds number of as, bs, cs, ds descending. can achieve drawing random numbers have constant sum (effectively drawing 1 less) , sorting them. in simple approach need retry several times because numbers of as, bs, cs or ds cannot equal according specification.
code example:
% total length of sequence n = 10; % try again until found list of lengths has no equal entries ni = [0,0]; % long there 2 equal numbers inside while any(diff(ni) == 0) % 1 length can value n1 = round(rand()*n); % remaining n2 = round(rand()*(n-n1)); % remaining n3 = round(rand()*(n-n1-n2)); % remaining n4 = n - n1 - n2 - n3; % sort ni = sort([n1, n2, n3, n4]); end % translate a, b, c, d r = [repmat('a', 1, ni(4)), repmat('b', 1, ni(3)), repmat('c', 1, ni(2)), repmat('d', 1, ni(1))]; r % random permuatation s = r(randperm(length(r))); s
gives:
aaaaaaabbc aabaabacaa
where first 1 ordered random sequence (showing indeed number of larger number of bs, ...) , second desired random sequence. translating bits trivial.
Comments
Post a Comment