资源描述:
《用c语言生成随机数报告.》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、Name:陈启武Studentnumber:2014200557Profession:MicroelectronicsTask1:GeneraterandomvariatesforGaussiandistriubutionanddrawthecorrespondingPDFs1、Theprincipal:a)Inprobabilitytheory,thenormal(orGaussian)distributionisaverycommonlyoccurringcontinuousprobabilitydistribution.Theprobabilityde
2、nsityfunctionofthenormaldistributionisTheparameterinthisdefinitionisthemeanorexpectationofthedistribution(andalsoitsmedianandmode).Theparameterisitsstandarddeviation;itsvarianceistherefore.ArandomvariablewithaGaussiandistributionissaidtobenormallydistributedandiscalledanormaldeviat
3、e.b)Themethodofgeneratingrandomnumbersistousethe“rand()”functiontogeneratearandomnumberinC++programfirstly,thenthegeneratedrandomnumberismappedtotherequiredrange.Inthetext,Igenerated10,000randomnumbersfollowedbyuniformdistributionbetweentheinterval[-10,10].c)Wecanusethegeneratedran
4、domnumberstocalculatedensityfunctionvalues,thesevaluesaretherequiredrandomnumbersfornormaldistributon.Inthetest,Idefined“normal()”functiontogenerate10000idensityfunctionvalueswhichhavebeenputintoatextcalled”123”.d)Finally,wemustdrawthecorrespondingPDFstotestandverifyprobabilitydens
5、ityfunctionvalueswithMATLAB.Inthetest,IIputtheprogrammeddatasinto500groupsandgetthedifferenthistogramsfornormal(0,1),(0,4.26),(-2.5,1).Here’sthecommandsofmatlabIhaveusedinthetest.m=load('F:textormal123.txt')hist(m,500)Title(‘N(miu,sigma)’)2、TheC++sourceprogram:#include#
6、include#include#include#includeusingnamespacestd;#definepi3.1415926//Generatearandonnumberfollowedbyuniformdistributionbetweentheinteval[min,max]templateTrand(Tmin,Tmax){returnmin+(max-min)*rand()/(RAND_MAX+1.0);}//calculatethenormald
7、ensitydistributionvaluesdoublenormal(doublex,doublemiu,doublesigma){return1.0/sqrt(2*pi)/sigma*exp(-1*(x-miu)*(x-miu)/(2*sigma*sigma));}//Generateadistributedrandomoutputnumberdoublerandn(doublemiu,doublesigma,doublemin,doublemax){doublex,y,dScope;do{x=rand(min,max);y=normal(x,miu,
8、sigma);dScope=rand(0.0,nor