Random.mesaProducesarandomsequenceofINTs.Lasteditedby:MBrownonAugust27,198210:59amRandom:CEDARDEFINITIONS=BEGINInit:PROC[range:INT_0,seed:INT_0]RETURNS[trueSeed:INT];TheparametersrangeandseeddeterminethesequencegeneratedbyprocedureNext(alsoChoose)below.Ifrange<=0,Nextwillproduceresultsin[0..LAST[INT]];otherwise,Nextwillproduceresultsin[0..range).Ifseed=0,adefaultseedvalueisusedtodeterminethestartingpointofthesequence;ifseed>0,seedisscaledifnecessaryandthenused;ifseed<0,aseedvalueisderivedfromthesystemclock.Inanycase,theseedvalueactuallyused(afterscaling)istheINTvaluereturnedbyInit.Avoidsmallvaluesoftherangeparameter;theintentofrangeisonlytoallowthesamesequencetobeproducedbydifferentmachines.Next:PROC[]RETURNS[INT];AcalltoNextreturnsthenextterminthesequencedeterminedbythemostrecentcalltoInit.Choose:PROC[min,max:INT]RETURNS[INT--[min..max]--];!Error[badInterval]([min..max]isanillegalinterval).Choosesapointintheinterval[min..max]atrandom.(Thechoiceisquiterandom,evenifthisintervalislarge.)Iftheintervalisempty(min>max),oriftheintervallength(max-min+1)exceedstherangeofnumbersthegeneratorwasinitializedtoproduce,raisesERRORError[badInterval].Error:ERROR[ec:ErrorType];ErrorType:TYPE={badInterval};END.RandomnumbergeneratorsBasicinformationAgeneratoriscreatedbymoduleinstantiation,thatis,itholdsthestateofitscurrentpseudo-randomsequenceinaglobalframe.Ageneratorisalsoamodulemonitor,sothattwounsynchronizedprocessesmayeachrequestthenexttermofthecurrentsequencewithnoilleffect.(Atestprogramthattakesadvantageofthisfeatureisnotlikelytogiverepeatableresults,however.)STARTingageneratorcausesittoinitializeitselfbycallingInit,withdefaultparameters.Soasimpleapplicationneedinga(repeatable)randomsequenceneednotcontainanexplicitInitcall.Threevariantsofthispackageareavailable,generatingnonnegativeINTs(Random),nonnegativeINTEGERs(RandomInt),andCARDINALs(RandomCard).TheonlyfunctionaldifferencebetweenthethreeinterfacesRandom,RandomCard,andRandomIntisthatRandomCardisonlycapableofgeneratingsequencesin[0..LAST[CARDINAL]];RandomCard.Inittakesnorangeparameter.BecauseInitisaprocedurehavingtwoparametersofthesametype,itisbestcalledbyusingMesa'skeywordparametersyntax;Chooseshouldalsobe.1p a q ]xvr]x]xq+]x]x [YH#s VtVkV s Vt Vu SsStSSsSStJSSsSMSt mS Ss"S$oSt&S&Ss+S,\St2S2Ss4S5Sq P~ &I(.46=F@DW NdQr$(*r,aN-$Nq/XNr/Nq1N2FN3 :=@6ES M0 xR #'g(+-v35:>B?B&H9 KNk #&+,1C259@:?}BE; InL+wf & ),134r5I5Iq7I8I;@B GQgo S %(#+-1V257P:=#@F G Eu B1sB1tB1B1suB1B1tB1B1sB1tB1sB1B1q ? $"F#&8+ 246:>&@Bau <)sW<)tU<)<)s<)D<)tt<)[<)s<)t<)<)s$<)t%9<)q'D<)'<) s/{<)/<)q:m-  ":#%) 74]c| %F&,/35=8=@BD 65 Yv"$')*-15 >CE 4  VY $2%+;r.4/4q3d44W4s 1t*11s1 1G /Y t//Y/Ys/Yk/Y t +s+p &uT v "k q  e<D' '|*|,m-14"79U;_@: xO##$'y(-247:= C  N"I',0238:=C F )G y "6% +r0r q xo8 $(R*P.2 5*9 ADE_   f#T(,.3P5C:< b _a $O * r2Fb2bq4Wb5Qb; rCZbCbqHb  jrq3> &\)", 2 8=@C  N"$)' /1 4(9%: AG lr l }lqlr lqCll!%w'j+- S1, " )+.15!68?;0?>AEE  5Dk*#p&=w)TVm$SampleprogramThefollowingistheskeletonofaprogramthatusestheRandompackage.Theprogramfillsanarraycalled"rolls"withrandomnumbersthatmightrepresentdietrials:DIRECTORYRandomUSING[Init,Choose],...Sample:PROGRAMIMPORTSRandom,......rolls:ARRAY[0..100)OFINT[1..6];...[]_Random.Init[];--usedefaultseedandfullrangeFORi:INTIN[0..100)DORolls[i]_Random.Choose[min:1,max:6]ENDLOOP;...END.TechnicaldetailsThesegeneratorsarebasedonanAlgolWprogramdistributedbyDonaldKnuthtohisCS144bclassin1975.TheyusetheadditiverandomnumbergenerationalgorithmthatisrecommendedinthesecondeditionofSeminumericalAlgorithmsbyKnuth,andthatwasthesubjectofaPh.D.thesisbyJohnReiserofStanford("Theanalysisofadditiverandomnumbergenerators",STAN-CS-77-601,March1977.)Theadditivegeneratorhasseveraladvantagesoverastandardlinearcongruentialgenerator(lcg):Thesequencesthatitgeneratesaremore"random"inseveralways.Withanlcggeneratingasequencea(n),therelatedsequencea(n)mod2hasperiod2,a(n)mod4hasperiod4,andsoon;thuscaremustbetakennottoderiveanyresultsprimarilyfromtheleast-significantbitsofa(n).Anadditivegeneratorproducesnumberswhosebitsaremoreuniformlyrandom.Anlcgforashortwordlengthhasashortperiod(forexample,asequenceof16bitcardinalsmustcycleafter64kterms);anadditivegeneratormayhaveaverylongperiodevenforshortwordlengths.Manylcgs(includingIBM'snotoriousRANDU)generatesequencesthatareverypoorlydistributedintwoandthreedimensions(youmightseethisbyplottingconsecutivepairsortriplesoftermsineuclideanspaceandnotingthepatterns);additivegeneratorsdonothavethisproblem.Thegeneratorrelativelyeasytotransportfrommachinetomachine,andcanbemadetogeneratethesamesequencesoneachmachine.Thiscanbedifficultwithlcgsbecausethesequencestheygenerateusuallydependonthewayamachinehandlesoverflows(inparticular,onthewordlength.)Anadditivegeneratorcanproducearandomsequencewithoutevercausinganoverflow.Thegeneratorisrelativelyfastbecauseitdoesnotusemultiplication,whichisatime-consumingoperationonourpresentmachines.ChangeLogCreatedbyMBrownonJuly2,1979ChangedbyMBrownonMay2,198011:18PMRenamedmoduletoconformtoCedarstandards.AddeddefaultingofparameterstoInitRandom,andnameditsresulttrueSeed.ChangedbyMBrownonSeptember25,19809:26AM2v aq ^ C$'*,2y8;A0CE \ 1-"W&1+.%r Zaq Xr\X9Xq XzX W UYr;UYUYvq6UY4UY  S QrqQ $)~ 042(7 ;Z=?[DG = o H!g&| -L368> @BE1 ;cf J C""&)],3.1)57U8<@=BEk 9 +7 %* 2R=B(F 8f "%&,V0/ 7= 5 LL#2)+N/479; BC 3 8 #')A,5/=037(8;t=;@GCGFC 2? m$'*D368I<+>{C 0p: &,.13!4J7 >@BEw . M.$'+J.166g8a=iCqFh -I ,.Rq &}*u-8 37=D& +@A+ #%(+\. 6 9I=T?BD ) 7) &*H,13P 9> EPG+ (S  %  _O"%+,,2u579[<>CF% $%*8z!#(+.36#C  zI""$)+ O  t!i"%("*3h7I89 C  Up x pd^ B" t%&sq c'"$ )X- 4,5 => F @x 5 k"&st) 5*U 5w)qTVm$AddedChoose.ChangedbyMBrownonMarch9,198210:47amAddedError,ErrorType(usedtoraiseunnamedERROR).ChangedbyMBrownonJune8,198211:04amChangedmodulenamefromRandomLongInt,procsNextfromRandom,InitfromInitRandom,mademoduleCEDARChangedbyMBrownonAugust27,198211:10amMergedindocumentation.3q b&x _x"&}q ]0'=!r&]0']0q*]0+f]0x Zd!%iq X: &*.!17:h= E VrVmVx TC #'q Q w)TVm$ TIMESROMAN HELVETICA TIMESROMANY TIMESROMAN TIMESROMANLAUREL TIMESROMAN TIMESROMAN TIMESROMAN j/ Random.press27-Aug-82 15:29:58