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 <k0qpfl$a6e$1@newscl01ah.mathworks.com>...
> 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 <none@non.net> wrote in message <k0qqhm$95t$1@speranza.aioe.org>...
> 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

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

rssFeed for this Thread

Contact us