MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

# Thread Subject: binary random number generator

 Subject: binary random number generator From: eng Date: 19 Aug, 2012 13:25:09 Message: 1 of 6 Hello every one, How generator a 8 bits binary random number contents two "1" bits and six "0" bits, for example : 1 1 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 0 1 1 0  ect. regards
 Subject: binary random number generator From: dpb Date: 19 Aug, 2012 13:43:16 Message: 2 of 6 On 8/19/2012 8:25 AM, eng wrote: > Hello every one, How generator a 8 bits binary random number contents > two "1" bits and six "0" bits, for example : > 1 1 0 0 0 0 0 0 > 1 0 1 0 0 0 0 0 > 0 0 0 0 0 1 1 0 > ect. >  >> N=3;for i=1:N,x=zeros(1,8);ix=randperm(8);x(ix(1:2))=1;disp(x),end       0 0 0 1 0 0 0 1       0 0 0 0 0 1 1 0       0 0 1 0 0 0 0 1  >> Above can be vectorized more thoroughly, of course... --
 Subject: binary random number generator From: Bruno Luong Date: 19 Aug, 2012 15:20:07 Message: 3 of 6 "eng" wrote in message ... > Hello every one, How generator a 8 bits binary random number contents two "1" bits and six "0" bits, for example : > 1 1 0 0 0 0 0 0 > 1 0 1 0 0 0 0 0 > 0 0 0 0 0 1 1 0 % Generate 100 of such numbers dec2bin(floor(2^8*rand(1,100)))-'0' % Bruno
 Subject: binary random number generator From: Bruno Luong Date: 19 Aug, 2012 15:43:07 Message: 4 of 6 Oops sorry, I missread the question that you want just two 1s. Bruno
 Subject: binary random number generator From: Bruno Luong Date: 19 Aug, 2012 15:57:06 Message: 5 of 6 % A new matlab has a second parameter for truncated randperm: n = 100; r = zeros(n,8); for k=1:n   r(k,randperm(8,2)) = 1; end % Or here is a vectorized code [~, col]=sort(rand(n,8),2); col = col(:,1:2); row = repmat((1:n)',[1 2]); r = accumarray([row(:) col(:)],1,[n 8]); % Bruno
 Subject: binary random number generator From: eng Date: 19 Aug, 2012 18:31:06 Message: 6 of 6 dpb wrote in message ... > On 8/19/2012 8:25 AM, eng wrote: > > Hello every one, How generator a 8 bits binary random number contents > > two "1" bits and six "0" bits, for example : > > 1 1 0 0 0 0 0 0 > > 1 0 1 0 0 0 0 0 > > 0 0 0 0 0 1 1 0 > > ect. > > > > >> N=3;for i=1:N,x=zeros(1,8);ix=randperm(8);x(ix(1:2))=1;disp(x),end > 0 0 0 1 0 0 0 1 > 0 0 0 0 0 1 1 0 > 0 0 1 0 0 0 0 1 > >> > > Above can be vectorized more thoroughly, of course... > > -- Thank you very much "dpb" with best regards