```Path: news.mathworks.com!newsfeed-00.mathworks.com!newsfeed2.dallas1.level3.net!news.level3.com!postnews.google.com!l14g2000vbe.googlegroups.com!not-for-mail
From: Jomar Bueyes <jomarbueyes@hotmail.com>
Newsgroups: comp.soft-sys.matlab
Subject: Re: coin flip
Date: Thu, 23 Feb 2012 07:52:39 -0800 (PST)
Lines: 36
References: <ji5m0o\$le3\$1@newscl01ah.mathworks.com>
NNTP-Posting-Host: 140.247.106.190
Mime-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
X-Trace: posting.google.com 1330013805 1119 127.0.0.1 (23 Feb 2012 16:16:45 GMT)
NNTP-Posting-Date: Thu, 23 Feb 2012 16:16:45 +0000 (UTC)
User-Agent: G2/1.0
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: news.mathworks.com comp.soft-sys.matlab:758723

On Feb 23, 10:29 am, "Kirk" <kwythers.nos...@umn.edu> 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.
E.g

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