欢迎来到天天文库
浏览记录
ID:34346856
大小:74.50 KB
页数:4页
时间:2019-03-05
《simulation lab仿真实验室》由会员上传分享,免费在线阅读,更多相关内容在应用文档-天天文库。
1、Monte-CarloSimulationsinJavaWrittenbyRichardFlintandJudyWaltersIntroductionManynaturalprocessesinvolveanelementofrandomchoice:weatherpatterns,spreadofdisease,andinheritanceofcertainbiologicaltraits.Engineeredprocessesarealsofrequentlyrandom,forinstancelotteriesandothergamesofchance.Mathemati
2、ciansmodelsuchprocessesbydefiningnumericvariablestostandfortherandomquantitiesofinterest.ThetechniqueofassigningrandomnumberstothesevariablesiscalledMonteCarlosimulation.OurgoalinthislabistostudysimpleMonteCarloproblemsthatcanbesolvedwithshortJavaprograms.WhenusingacomputerlanguagelikeJavato
3、performaMonteCarlosimulation,you’llneedthecomputertogeneraterandomnumbers.Thatis,insteadofassigningvariablevalueswithinthecodeorfromtheuser,you’llcreateaRandomobjectwhichhasmethodsforreturningrandomnumbersinadesignatedrange.Todothisyouwillneedtoimportthejava.util.Randomlibrary.Technically,th
4、evaluesreturnedbytheRandomobjectaren’trandominamathematicalsense,butratheraregeneratedbyacomplexalgorithminvolvingthesystem’sinternaltimerepresentation.Howevertheyarerandominthesensethatthereisanevenprobabilityofgettinganygivennumberinthedesignatedrangeandtherearenodetectablepatternstotheord
5、erinwhichnumbersaregenerated.HereishowyouwouldcreateaRandomobjectnamedrand:Randomrand=newRandom();Andherearetwodifferentmethodsyoucouldusetomakeitreturnarandomnumberbetween0and9.Ineachcasetheresultisbeingstoredinanintegervariablecallednum.num=rand.nextInt(10);//Rangesizeis10beginningwith0num
6、=rand.nextInt(9999)%10;Ifyouwantthesetofpossiblenumberstostartwithanyvalueotherthan0,yousimplyaddthedesiredstartingvaluetothereturnedrandomvalue.Forexample,bothofthefollowingstatementsassignnumarandomnumberbetween1and10.num=rand.nextInt(10)+1;//Rangesizeis10beginningwith1num=rand.nextInt(999
7、9)%10+1;Eitherofthefollowingstatementswouldassignnumarandomvaluebetween-10and-5(arangeof6numbers).num=rand.nextInt(6)-10;//Rangesizeis6beginningwith-10num=rand.nextInt(9999)%6-10;ForPracticeWritestatementstogeneraterandomintegervalueswithineachofth
此文档下载收益归作者所有