Path: news.mathworks.com!not-for-mail
From: Alan_Weiss <aweiss@mathworks.com>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Generating sequence of 0s and 1s with no repetition
Date: Mon, 22 Jul 2013 14:30:08 -0400
Organization: MathWorks
Lines: 34
Message-ID: <ksjtng$4i6$1@newscl01ah.mathworks.com>
References: <ksjqaq$omm$1@newscl01ah.mathworks.com> <ksjruv$sd4$1@newscl01ah.mathworks.com>
Reply-To: aweiss@mathworks.com
NNTP-Posting-Host: ah-aweiss.dhcp.mathworks.com
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
X-Trace: newscl01ah.mathworks.com 1374517808 4678 172.28.17.56 (22 Jul 2013 18:30:08 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Mon, 22 Jul 2013 18:30:08 +0000 (UTC)
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:17.0) Gecko/20130620 Thunderbird/17.0.7
In-Reply-To: <ksjruv$sd4$1@newscl01ah.mathworks.com>
Xref: news.mathworks.com comp.soft-sys.matlab:799740

On 7/22/2013 1:59 PM, Alan_Weiss wrote:
> On 7/22/2013 1:32 PM, Vitor wrote:
>> Hello.
>> I need to generate a sequence of 100 numbers composed of 0s and 1s only.
>> But I must guarantee that no more that 3 repetitions occur.
>> For xample:
>> 1 2 2 1 2 2 1 1 1 2 1... its ok
>> 1 2 1 2 2 2 2 1  it's not.
>> I used randi for the generation of the sequence. But i'm a bit stuuck 
>> with the remaining part. I'm sure there is an efficient way of 
>> achieving this.
>> Thanks for any help.
>
> I guess I would alternately sample between {0,00,000} and {1,11,111} 
> until you get to 100 digits.
>
> Alan Weiss
> MATLAB mathematical toolbox documentation

I was curious, so played with a script. This uses randsample from 
Statistics Toolbox, but you could cobble together your own sampling 
function easily:

z = {'0','00','000'};
p = {'1','11','111'};
rz = randsample(z,50,true); % gets at least 50 digits
rp = randsample(p,50,true);
r = [rp;rz]; % a 2-by-50 array
rr = r(:)'; % a 1-by-100 array of alternating samples
rout = strjoin(rr,''); % all the digits
rout = rout(1:100); % the first 100 digits

Alan Weiss
MATLAB mathematical toolbox documentation