Discover MakerZone

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

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
coin flip

Subject: coin flip

From: Kirk

Date: 23 Feb, 2012 15:29:28

Message: 1 of 6

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.

Subject: coin flip

From: dpb

Date: 23 Feb, 2012 15:57:20

Message: 2 of 6

On 2/23/2012 9: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.

Simpler than

if rand(1)>0.5
   ..
else
   ..
end

?

-

Subject: coin flip

From: Asbjorn

Date: 23 Feb, 2012 16:00:16

Message: 3 of 6

"Kirk" <kwythers.nospam@umn.edu> wrote in message <ji5m0o$le3$1@newscl01ah.mathworks.com>...
> 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 want it to be random I'd say you have to use the "rand" function. Maybe someone else knows a more elegant method but my suggestion would be:

if round(rand(1))

else

end

Subject: coin flip

From: someone

Date: 23 Feb, 2012 16:01:15

Message: 4 of 6

"Kirk" <kwythers.nospam@umn.edu> wrote in message <ji5m0o$le3$1@newscl01ah.mathworks.com>...
> 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.

doc rand

% From the first Example:
% Make a random choice between two equally probable alternatives:

if rand < .5
    'heads'
 else
    'tails'
 end

Subject: coin flip

From: Jomar Bueyes

Date: 23 Feb, 2012 15:52:39

Message: 5 of 6

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
   heads = heads + 1;
else
   tails = tails + 1;
end

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

HTH

Jomar

Subject: coin flip

From: Steven_Lord

Date: 23 Feb, 2012 18:16:44

Message: 6 of 6



"Kirk" <kwythers.nospam@umn.edu> wrote in message
news:ji5m0o$le3$1@newscl01ah.mathworks.com...
> 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.

Use Example 3 in the documentation for RANDI as a model. You want integer
values drawn uniformly from 0:1.

http://www.mathworks.com/help/techdoc/ref/randi.html

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Tags for this Thread

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.

Contact us