lecture5-6

Thento

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: s and 5’s. dice = randi(6,1,ndice); baddice = sum(dice == 2) + sum(dice == 5); score = sum(dice) * (baddice == 0); end To
play
a
game,
we
simply
call
rolldice
until
the
number
of
dice
remaining
is
zero.

Tis
function
returns
the
grand
 total
at
the
end:
 
 function [ total ] = dropdead( ) %dropdead plays a game of dropdead and % returns the final score n = 5; total = 0; while n > 0 [score baddice] = rolldice(n); n = n - baddice; total = total + score; end end Our
actual
interest,
however
was
to
get
some
idea
of
how
scores
for
games
are
distributed,
i.e.
let’s
play
100000
 games
and
plot
the
number
of
times
each
score
comes
up.

That’s
easily
done
by
setting
up
a
vector
to
be
the
bin
 counter
for
the
games.

Each
time
a
game
comes
back
with
score
=
X,
we
will
increment
the
value
in
box(X).

One
 slight
difficulty
with
Matlab
is
vectors
don’t
have
a
box
0,
so
we’ll
store
the
results
shifted
over
by
1.

That
is,
the
 results
for
score
=
0
will
be
stored
in
the
1
box,
the
result
for
score
=
1
will
be
stored
in
box
...
View Full Document

This document was uploaded on 03/16/2014 for the course CS 106 at Mt. Holyoke.

Ask a homework question - tutors are online