Python Programming: An Introduction to Computer Science

(Nora) #1
9.6.EXERCISES 153

Writea programthatsimulatesmultiplegamesofblackjackandestimatestheprobabilitythatthedealer
willbust.


  1. Ablackjackdealeralwaysstartswithonecardshowing.It wouldbeusefulfora playertoknowthe
    dealer’s bustprobability(seepreviousproblem)foreachpossiblestartingvalue.Writea simulation
    programthatrunsmultiplehandsofblackjackforeachpossiblestartingvalue(ace–10)andestimates
    theprobabilitythatthedealerbustsforeachstartingvalue.

  2. MonteCarlotechniquescanbeusedtoestimatethevalueofpi.Supposeyouhave a rounddartboard
    thatjustfitsinsideofa squarecabinet. If youthrowdartsrandomly, theproportionthathitthedart
    boardvs.thosethathitthecabinet(inthecornersnotcoveredbytheboard)willbedeterminedbythe
    relative areaofthedartboardandthecabinet.Ifnis thetotalnumberofdartsrandomlythrown(that
    landwithintheconfinesofthecabinet),andhis thenumberthathittheboard,it is easytoshow that


π 4

h
n



Writea programthatacceptsthe“numberofdarts”asaninputandthenperformsa simulationto
estimateπ. Hint:youcanuse2*random() - 1togeneratethexandycoordinatesofa random
pointinsidea 2x2squarecenteredat


0  0 . Thepointliesinsidetheinscribedcircleifx^2  y^2 1.


  1. Writea programthatperformsa simulationtoestimatetheprobabilityofrollingfive-of-a-kindina
    singlerolloffive six-sideddice.

  2. Arandomwalkis a particularkindofprobablisticsimulationthatmodelscertainstatisticalsystems
    suchastheBrownianmotionofmolecules.Youcanthinkofa one-dimensionalrandomwalkinterms
    ofcoinflipping.Supposeyouarestandingona verylongstraightsidewalkthatextendsbothinfront
    ofandbehindyou.Youflipa coin.If it comesupheads,youtake a stepforward;tailsmeanstotake a
    stepbackward.
    Supposeyoutake a randomwalkofnsteps.Onaverage,how many stepsawayfromthestartingpoint
    willyouendup?Writea programtohelpyouinvestigatethisquestion.

  3. Supposeyouaredoinga randomwalk(seepreviousproblem)ontheblocksofa citystreet.Ateach
    “step”youchoosetowalkoneblock(atrandom)eitherforward,backward,leftorright. Innsteps,
    how fardoyouexpecttobefromyourstartingpoint?Writea programtohelpanswerthisquestion.

  4. Writea graphicalprogramtotracea randomwalk(seeprevioustwo problems)intwo dimensions.
    Inthissimulationyoushouldallow thesteptobetakeninanydirection.Youcangeneratea random
    directionasanangleoff ofthexaxis.


angle = random()* 2 * math.pi

Thenewxandypositionsarethengivenbytheseformulas.

x = x + cos(angle)
y = y + sin(angle)

Theprogramshouldtake thenumberofstepsasaninput.Startyourwalkerat thecenterofa 100x100
gridanddraw a linethattracesthewalkasit progresses.


  1. (Advanced)Hereisa puzzleproblemthatcanbesolvedwitheithersomefancy analyticgeometry
    (calculus)ora (relatively)simplesimulation.
    Supposeyouarelocatedattheexactcenterofa cube. Ifyoucouldlookallaroundyouinevery
    direction,eachwallofthecubewouldoccupy^16 ofyourfieldofvision.Supposeyoumove towardone
    ofthewallssothatyouarenow half-waybetweenit andthecenterofthecube.Whatfractionofyour
    fieldofvisionis now takenupbytheclosestwall?Hint:usea MonteCarlosimulationthatrepeatedly
    “looks”ina randomdirectionandcountshow many timesit seesthewall.

Free download pdf