152 CHAPTER9. SIMULATIONANDDESIGN
9.6 Exercises
- Draw thetoplevelsofa structurechartfora programhavingthefollowingmainfunction.
def main():
printIntro()
length, width= getDimensions()
amtNeeded = computeAmount(length,width)
printReport(length,width, amtNeeded)
- Writeanexpressionusingeitherrandomorrandrangetocalculatethefollowing.
A randomintintherange0–10
A randomfloatintherange-0.5–0.5
A randomnumberrepresentingtherollofa six-sideddie
A randomnumberrepresentingthesumresultingfromrollingtwo six-sideddice
A randomfloatintherange-10.0–10.0
- Revisetheracquetballsimulationsothatit keepstrackofresultsforbestofngamematches.
- Revisetheracquetballsimulationtotake shutoutsintoaccount.Yourupdatedversionshouldreportfor
bothplayersthenumberofwins,percentageofwins,numberofshutouts,andpercentageofwinsthat
areshutouts.
- Designandimplementa simulationofthegameofvolleyball.Normalvolleyballis playedlike rac-
quetball,inthata teamcanonlyscorepointswhenit is serving.Gamesareplayedto15,butmustbe
wonbyat leasttwo points.
- Collegevolleyballisnowplayedusingrallyscoring. Inthissystem,theteamthatwinsa rallyis
awardeda point,evenif they werenottheservingteam.Gamesareplayedtoa scoreof21.Design
andimplementa simulationofvolleyballusingrallyscoring.
- Designandimplementa systemthatcomparesregularvolleyballgamestothoseusingrallyscoring.
Yourprogramshouldbeabletoinvestigatewhetherrallyscoringmagnifies,reduces,orhasnoeffect
ontherelative advantageenjoyedbythebetterteam.
- Designandimplementa simulationofsomeotherracquetsport(e.g.tennisortabletennis).
- Crapsis a dicegameplayedatmany casinos.Aplayerrollsa pairofnormalsix-sideddice.If the
initialrollis 2,3 or12,theplayerloses.If therollis 7 or11,theplayerwins.Any otherinitialroll
causestheplayerto“rollforpoint.” Thatis,theplayerkeepsrollingthediceuntileitherrollinga 7 or
re-rollingthevalueoftheinitialroll.If theplayerre-rollstheinitialvaluebeforerollinga 7, it’s a win.
Rollinga 7 firstis a loss.
Writea programtosimulatemultiplegamesofcrapsandestimatetheprobabilitythattheplayerwins.
Forexample,if theplayerwins 249 outof 500 games,thentheestimatedprobabilityofwinningis
249
500 0 498
- Blackjack(twenty-one)is a casinogameplayedwithcards.Thegoalofthegametodrawcardsthat
totalascloseto 21 pointsaspossiblewithoutgoingover. Allfacecardscountas 10 points,acescount
as1 or11,andallothercardscounttheirnumericvalue.
Thegameisplayedagainsta dealer. Theplayertriestogetcloserto 21 (withoutgoingover)than
thedealer. If thedealerbusts(goesover21)theplayerautomaticallywins(providedtheplayerhad
notalreadybusted).Thedealermustalwaystake cardsaccordingtoa fixedsetofrules.Thedealer
takescardsuntilheorsheachievesa totalofat least17.If thedealer’s handcontainsanace,it willbe
countedas 11 whenthatresultsina totalbetween 17 and 21 inclusive; otherwise,theaceis countedas