Python Programming: An Introduction to Computer Science

(Nora) #1
138 CHAPTER9. SIMULATIONANDDESIGN

9.1.2 ProgramSpecification


Racquetballis a sportplayedbetweentwo playersusingracquetstostrike a ballina four-walledcourt.It
hasaspectssimilartomany otherballandracquetgamessuchastennis,volleyball,badminton,squash,table
tennis,etc.We don’t needto understandalltherulesofracquetballto writetheprogram,justthebasicoutline
ofthegame.
To startthegame,oneoftheplayersputstheballintoplay—thisiscalledserving. Theplayersthen
alternatehittingtheballtokeepit inplay;thisis arally. Therallyendswhenoneoftheplayersfailstohita
legalshot.Theplayerwhomissestheshotlosestherally. If theloseris theplayerwhoserved,thenservice
passesto theotherplayer. If theserver winstherally, a pointis awarded.Playerscanonlyscorepointsduring
theirservice.Thefirstplayertoreach 15 pointswinsthegame.
Inoursimulation,theability-leveloftheplayerswillberepresentedbytheprobabilitythattheplayer
winstherallywhenheorsheserves.Thus,playerswitha 0.6probabilitywina pointon60%oftheirserves.
Theprogramwillprompttheusertoentertheserviceprobabilityforbothplayersandthensimulatemultiple
gamesofracquetballusingthoseprobabilities.Theprogramwillthenprinta summaryoftheresults.
Hereis a detailedspecification:


InputTheprogramfirstpromptsforandgetstheserviceprobabilitiesofthetwo players(called“PlayerA”
and“PlayerB”).Thentheprogrampromptsforandgetsthenumberofgamestobesimulated.


OutputTheprogramwillprovidea seriesofinitialpromptssuchasthefollowing:


What is the prob.player A wins a serve?
What is the prob.player B wins a serve?
How many games to simulate?

Theprogramwillprintouta nicelyformattedreportshowingthenumberofgamessimulatedandthe
numberofwinsandwinningpercentageforeachplayer. Hereis anexample:

Games Simulated: 500
Wins for A: 268 (53.6%)
Wins for B: 232 (46.4%)

Notes:Allinputsareassumedtobelegalnumericvalues,noerrororvaliditycheckingis required.


Ineachsimulatedgame,playerA servesfirst.

9.2 RandomNumbers.


Oursimulationprogramwillhave todealwithuncertainevents.Whenwesaythata playerwins50%ofthe
serves,thatdoesnotmeanthateveryotherserve is a winner. It’s morelike a cointoss.Overall,weexpect
thathalfthetimethecoinwillcomeupheadsandhalfthetimeit willcomeuptails,butthereis nothingto
preventa runoffive tailsina row. Similarly, ourracquetballplayershouldwinorloseralliesrandomly. The
serviceprobabilityprovidesa likelihoodthata givenserve willbewon,butthereis nosetpattern.
Many simulationssharethispropertyofrequiringeventstooccurwitha certainlikelihood. Adriving
simulationmustmodeltheunpredictabilityofotherdrivers;a banksimulationhastodealwiththerandom
arrival ofcustomers.ThesesortsofsimulationsaresometimescalledMonteCarloalgorithms,becausethe
resultsdependon“chance”probabilities.Ofcourse,youknow thatthereis nothingrandomaboutcomputers;
they areinstruction-followingmachines.How cancomputerprogramsmodelseeminglyrandomhappenings?
Simulatingrandomnessis a well-studiedproblemincomputerscience.Rememberthechaosprogram
fromChapter1?Thenumbersproducedbythatprogramseemedto jumparoundrandomlybetweenzeroand
one.Thisapparentrandomnesscamefromrepeatedlyapplyinga functionto generatea sequenceofnumbers.
A similarapproachcanbeusedtogeneraterandom(actuallypseudorandom) numbers.
A pseudorandomnumbergeneratorworksbystartingwithsomeseedvalue.Thisvalueis fedto a function
toproducea “random”number. Thenexttimea randomnumberis needed,thecurrentvalueis fedbackinto

Free download pdf