From: Jomar Bueyes <>
Newsgroups: comp.soft-sys.matlab
Subject: Re: coin flip
Date: Thu, 23 Feb 2012 07:52:39 -0800 (PST)
Lines: 36
Message-ID: <>
References: <ji5m0o$le3$>
Mime-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
X-Trace: 1330013805 1119 (23 Feb 2012 16:16:45 GMT)
NNTP-Posting-Date: Thu, 23 Feb 2012 16:16:45 +0000 (UTC)
Injection-Info:; posting-host=; posting-account=UQjN4QoAAABmrUsSpLGrPgqLI4psJPvG
User-Agent: G2/1.0
X-Google-Web-Client: true
X-Google-Header-Order: HUALESNKRC
X-HTTP-UserAgent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.5; rv:9.0.1)
 Gecko/20100101 Firefox/9.0.1,gzip(gfe)
Xref: comp.soft-sys.matlab:758723

On Feb 23, 10:29 am, "Kirk" <> wrote:
> I need a suggestion on how to randomly generate 2 values (0 or 1), that can be used in an algorithm to perform an "either / or" action.
> As in:
> If 0, do this,
> If 1, do that.
> I've been looking at random number control, but there must be a more simple approach.

If you strictly want outcomes that are either 0 or 1 and are equally
probable, you could use the floor function in combination with rand.

outcome =  floor(2*rand)

will always result in ones and zeros and, on average, there will be as
many ones as zeros (note the ON AVERAGE).
However, there are simpler and more flexible ways to simulate coin
tossing (including simulating an unfair coin). If the probability of a
head is p, then you could do something like

if rand <= p
   heads = heads + 1;
   tails = tails + 1;

If you have the statistics toolbox, you could use the multrnd function
to generate binomial pseudo random numbers