random.h 174 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111
  1. // random number generation -*- C++ -*-
  2. // Copyright (C) 2009-2021 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /**
  21. * @file bits/random.h
  22. * This is an internal header file, included by other library headers.
  23. * Do not attempt to use it directly. @headername{random}
  24. */
  25. #ifndef _RANDOM_H
  26. #define _RANDOM_H 1
  27. #include <vector>
  28. #include <bits/uniform_int_dist.h>
  29. namespace std _GLIBCXX_VISIBILITY(default)
  30. {
  31. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  32. // [26.4] Random number generation
  33. /**
  34. * @defgroup random Random Number Generation
  35. * @ingroup numerics
  36. *
  37. * A facility for generating random numbers on selected distributions.
  38. * @{
  39. */
  40. // std::uniform_random_bit_generator is defined in <bits/uniform_int_dist.h>
  41. /**
  42. * @brief A function template for converting the output of a (integral)
  43. * uniform random number generator to a floatng point result in the range
  44. * [0-1).
  45. */
  46. template<typename _RealType, size_t __bits,
  47. typename _UniformRandomNumberGenerator>
  48. _RealType
  49. generate_canonical(_UniformRandomNumberGenerator& __g);
  50. /*
  51. * Implementation-space details.
  52. */
  53. namespace __detail
  54. {
  55. template<typename _UIntType, size_t __w,
  56. bool = __w < static_cast<size_t>
  57. (std::numeric_limits<_UIntType>::digits)>
  58. struct _Shift
  59. { static const _UIntType __value = 0; };
  60. template<typename _UIntType, size_t __w>
  61. struct _Shift<_UIntType, __w, true>
  62. { static const _UIntType __value = _UIntType(1) << __w; };
  63. template<int __s,
  64. int __which = ((__s <= __CHAR_BIT__ * sizeof (int))
  65. + (__s <= __CHAR_BIT__ * sizeof (long))
  66. + (__s <= __CHAR_BIT__ * sizeof (long long))
  67. /* assume long long no bigger than __int128 */
  68. + (__s <= 128))>
  69. struct _Select_uint_least_t
  70. {
  71. static_assert(__which < 0, /* needs to be dependent */
  72. "sorry, would be too much trouble for a slow result");
  73. };
  74. template<int __s>
  75. struct _Select_uint_least_t<__s, 4>
  76. { typedef unsigned int type; };
  77. template<int __s>
  78. struct _Select_uint_least_t<__s, 3>
  79. { typedef unsigned long type; };
  80. template<int __s>
  81. struct _Select_uint_least_t<__s, 2>
  82. { typedef unsigned long long type; };
  83. #ifdef _GLIBCXX_USE_INT128
  84. template<int __s>
  85. struct _Select_uint_least_t<__s, 1>
  86. { typedef unsigned __int128 type; };
  87. #endif
  88. // Assume a != 0, a < m, c < m, x < m.
  89. template<typename _Tp, _Tp __m, _Tp __a, _Tp __c,
  90. bool __big_enough = (!(__m & (__m - 1))
  91. || (_Tp(-1) - __c) / __a >= __m - 1),
  92. bool __schrage_ok = __m % __a < __m / __a>
  93. struct _Mod
  94. {
  95. typedef typename _Select_uint_least_t<std::__lg(__a)
  96. + std::__lg(__m) + 2>::type _Tp2;
  97. static _Tp
  98. __calc(_Tp __x)
  99. { return static_cast<_Tp>((_Tp2(__a) * __x + __c) % __m); }
  100. };
  101. // Schrage.
  102. template<typename _Tp, _Tp __m, _Tp __a, _Tp __c>
  103. struct _Mod<_Tp, __m, __a, __c, false, true>
  104. {
  105. static _Tp
  106. __calc(_Tp __x);
  107. };
  108. // Special cases:
  109. // - for m == 2^n or m == 0, unsigned integer overflow is safe.
  110. // - a * (m - 1) + c fits in _Tp, there is no overflow.
  111. template<typename _Tp, _Tp __m, _Tp __a, _Tp __c, bool __s>
  112. struct _Mod<_Tp, __m, __a, __c, true, __s>
  113. {
  114. static _Tp
  115. __calc(_Tp __x)
  116. {
  117. _Tp __res = __a * __x + __c;
  118. if (__m)
  119. __res %= __m;
  120. return __res;
  121. }
  122. };
  123. template<typename _Tp, _Tp __m, _Tp __a = 1, _Tp __c = 0>
  124. inline _Tp
  125. __mod(_Tp __x)
  126. {
  127. if _GLIBCXX17_CONSTEXPR (__a == 0)
  128. return __c;
  129. else
  130. {
  131. // _Mod must not be instantiated with a == 0
  132. constexpr _Tp __a1 = __a ? __a : 1;
  133. return _Mod<_Tp, __m, __a1, __c>::__calc(__x);
  134. }
  135. }
  136. /*
  137. * An adaptor class for converting the output of any Generator into
  138. * the input for a specific Distribution.
  139. */
  140. template<typename _Engine, typename _DInputType>
  141. struct _Adaptor
  142. {
  143. static_assert(std::is_floating_point<_DInputType>::value,
  144. "template argument must be a floating point type");
  145. public:
  146. _Adaptor(_Engine& __g)
  147. : _M_g(__g) { }
  148. _DInputType
  149. min() const
  150. { return _DInputType(0); }
  151. _DInputType
  152. max() const
  153. { return _DInputType(1); }
  154. /*
  155. * Converts a value generated by the adapted random number generator
  156. * into a value in the input domain for the dependent random number
  157. * distribution.
  158. */
  159. _DInputType
  160. operator()()
  161. {
  162. return std::generate_canonical<_DInputType,
  163. std::numeric_limits<_DInputType>::digits,
  164. _Engine>(_M_g);
  165. }
  166. private:
  167. _Engine& _M_g;
  168. };
  169. template<typename _Sseq>
  170. using __seed_seq_generate_t = decltype(
  171. std::declval<_Sseq&>().generate(std::declval<uint_least32_t*>(),
  172. std::declval<uint_least32_t*>()));
  173. // Detect whether _Sseq is a valid seed sequence for
  174. // a random number engine _Engine with result type _Res.
  175. template<typename _Sseq, typename _Engine, typename _Res,
  176. typename _GenerateCheck = __seed_seq_generate_t<_Sseq>>
  177. using __is_seed_seq = __and_<
  178. __not_<is_same<__remove_cvref_t<_Sseq>, _Engine>>,
  179. is_unsigned<typename _Sseq::result_type>,
  180. __not_<is_convertible<_Sseq, _Res>>
  181. >;
  182. } // namespace __detail
  183. /**
  184. * @addtogroup random_generators Random Number Generators
  185. * @ingroup random
  186. *
  187. * These classes define objects which provide random or pseudorandom
  188. * numbers, either from a discrete or a continuous interval. The
  189. * random number generator supplied as a part of this library are
  190. * all uniform random number generators which provide a sequence of
  191. * random number uniformly distributed over their range.
  192. *
  193. * A number generator is a function object with an operator() that
  194. * takes zero arguments and returns a number.
  195. *
  196. * A compliant random number generator must satisfy the following
  197. * requirements. <table border=1 cellpadding=10 cellspacing=0>
  198. * <caption align=top>Random Number Generator Requirements</caption>
  199. * <tr><td>To be documented.</td></tr> </table>
  200. *
  201. * @{
  202. */
  203. /**
  204. * @brief A model of a linear congruential random number generator.
  205. *
  206. * A random number generator that produces pseudorandom numbers via
  207. * linear function:
  208. * @f[
  209. * x_{i+1}\leftarrow(ax_{i} + c) \bmod m
  210. * @f]
  211. *
  212. * The template parameter @p _UIntType must be an unsigned integral type
  213. * large enough to store values up to (__m-1). If the template parameter
  214. * @p __m is 0, the modulus @p __m used is
  215. * std::numeric_limits<_UIntType>::max() plus 1. Otherwise, the template
  216. * parameters @p __a and @p __c must be less than @p __m.
  217. *
  218. * The size of the state is @f$1@f$.
  219. */
  220. template<typename _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
  221. class linear_congruential_engine
  222. {
  223. static_assert(std::is_unsigned<_UIntType>::value,
  224. "result_type must be an unsigned integral type");
  225. static_assert(__m == 0u || (__a < __m && __c < __m),
  226. "template argument substituting __m out of bounds");
  227. template<typename _Sseq>
  228. using _If_seed_seq = typename enable_if<__detail::__is_seed_seq<
  229. _Sseq, linear_congruential_engine, _UIntType>::value>::type;
  230. public:
  231. /** The type of the generated random value. */
  232. typedef _UIntType result_type;
  233. /** The multiplier. */
  234. static constexpr result_type multiplier = __a;
  235. /** An increment. */
  236. static constexpr result_type increment = __c;
  237. /** The modulus. */
  238. static constexpr result_type modulus = __m;
  239. static constexpr result_type default_seed = 1u;
  240. /**
  241. * @brief Constructs a %linear_congruential_engine random number
  242. * generator engine with seed 1.
  243. */
  244. linear_congruential_engine() : linear_congruential_engine(default_seed)
  245. { }
  246. /**
  247. * @brief Constructs a %linear_congruential_engine random number
  248. * generator engine with seed @p __s. The default seed value
  249. * is 1.
  250. *
  251. * @param __s The initial seed value.
  252. */
  253. explicit
  254. linear_congruential_engine(result_type __s)
  255. { seed(__s); }
  256. /**
  257. * @brief Constructs a %linear_congruential_engine random number
  258. * generator engine seeded from the seed sequence @p __q.
  259. *
  260. * @param __q the seed sequence.
  261. */
  262. template<typename _Sseq, typename = _If_seed_seq<_Sseq>>
  263. explicit
  264. linear_congruential_engine(_Sseq& __q)
  265. { seed(__q); }
  266. /**
  267. * @brief Reseeds the %linear_congruential_engine random number generator
  268. * engine sequence to the seed @p __s.
  269. *
  270. * @param __s The new seed.
  271. */
  272. void
  273. seed(result_type __s = default_seed);
  274. /**
  275. * @brief Reseeds the %linear_congruential_engine random number generator
  276. * engine
  277. * sequence using values from the seed sequence @p __q.
  278. *
  279. * @param __q the seed sequence.
  280. */
  281. template<typename _Sseq>
  282. _If_seed_seq<_Sseq>
  283. seed(_Sseq& __q);
  284. /**
  285. * @brief Gets the smallest possible value in the output range.
  286. *
  287. * The minimum depends on the @p __c parameter: if it is zero, the
  288. * minimum generated must be > 0, otherwise 0 is allowed.
  289. */
  290. static constexpr result_type
  291. min()
  292. { return __c == 0u ? 1u : 0u; }
  293. /**
  294. * @brief Gets the largest possible value in the output range.
  295. */
  296. static constexpr result_type
  297. max()
  298. { return __m - 1u; }
  299. /**
  300. * @brief Discard a sequence of random numbers.
  301. */
  302. void
  303. discard(unsigned long long __z)
  304. {
  305. for (; __z != 0ULL; --__z)
  306. (*this)();
  307. }
  308. /**
  309. * @brief Gets the next random number in the sequence.
  310. */
  311. result_type
  312. operator()()
  313. {
  314. _M_x = __detail::__mod<_UIntType, __m, __a, __c>(_M_x);
  315. return _M_x;
  316. }
  317. /**
  318. * @brief Compares two linear congruential random number generator
  319. * objects of the same type for equality.
  320. *
  321. * @param __lhs A linear congruential random number generator object.
  322. * @param __rhs Another linear congruential random number generator
  323. * object.
  324. *
  325. * @returns true if the infinite sequences of generated values
  326. * would be equal, false otherwise.
  327. */
  328. friend bool
  329. operator==(const linear_congruential_engine& __lhs,
  330. const linear_congruential_engine& __rhs)
  331. { return __lhs._M_x == __rhs._M_x; }
  332. /**
  333. * @brief Writes the textual representation of the state x(i) of x to
  334. * @p __os.
  335. *
  336. * @param __os The output stream.
  337. * @param __lcr A % linear_congruential_engine random number generator.
  338. * @returns __os.
  339. */
  340. template<typename _UIntType1, _UIntType1 __a1, _UIntType1 __c1,
  341. _UIntType1 __m1, typename _CharT, typename _Traits>
  342. friend std::basic_ostream<_CharT, _Traits>&
  343. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  344. const std::linear_congruential_engine<_UIntType1,
  345. __a1, __c1, __m1>& __lcr);
  346. /**
  347. * @brief Sets the state of the engine by reading its textual
  348. * representation from @p __is.
  349. *
  350. * The textual representation must have been previously written using
  351. * an output stream whose imbued locale and whose type's template
  352. * specialization arguments _CharT and _Traits were the same as those
  353. * of @p __is.
  354. *
  355. * @param __is The input stream.
  356. * @param __lcr A % linear_congruential_engine random number generator.
  357. * @returns __is.
  358. */
  359. template<typename _UIntType1, _UIntType1 __a1, _UIntType1 __c1,
  360. _UIntType1 __m1, typename _CharT, typename _Traits>
  361. friend std::basic_istream<_CharT, _Traits>&
  362. operator>>(std::basic_istream<_CharT, _Traits>& __is,
  363. std::linear_congruential_engine<_UIntType1, __a1,
  364. __c1, __m1>& __lcr);
  365. private:
  366. _UIntType _M_x;
  367. };
  368. /**
  369. * @brief Compares two linear congruential random number generator
  370. * objects of the same type for inequality.
  371. *
  372. * @param __lhs A linear congruential random number generator object.
  373. * @param __rhs Another linear congruential random number generator
  374. * object.
  375. *
  376. * @returns true if the infinite sequences of generated values
  377. * would be different, false otherwise.
  378. */
  379. template<typename _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
  380. inline bool
  381. operator!=(const std::linear_congruential_engine<_UIntType, __a,
  382. __c, __m>& __lhs,
  383. const std::linear_congruential_engine<_UIntType, __a,
  384. __c, __m>& __rhs)
  385. { return !(__lhs == __rhs); }
  386. /**
  387. * A generalized feedback shift register discrete random number generator.
  388. *
  389. * This algorithm avoids multiplication and division and is designed to be
  390. * friendly to a pipelined architecture. If the parameters are chosen
  391. * correctly, this generator will produce numbers with a very long period and
  392. * fairly good apparent entropy, although still not cryptographically strong.
  393. *
  394. * The best way to use this generator is with the predefined mt19937 class.
  395. *
  396. * This algorithm was originally invented by Makoto Matsumoto and
  397. * Takuji Nishimura.
  398. *
  399. * @tparam __w Word size, the number of bits in each element of
  400. * the state vector.
  401. * @tparam __n The degree of recursion.
  402. * @tparam __m The period parameter.
  403. * @tparam __r The separation point bit index.
  404. * @tparam __a The last row of the twist matrix.
  405. * @tparam __u The first right-shift tempering matrix parameter.
  406. * @tparam __d The first right-shift tempering matrix mask.
  407. * @tparam __s The first left-shift tempering matrix parameter.
  408. * @tparam __b The first left-shift tempering matrix mask.
  409. * @tparam __t The second left-shift tempering matrix parameter.
  410. * @tparam __c The second left-shift tempering matrix mask.
  411. * @tparam __l The second right-shift tempering matrix parameter.
  412. * @tparam __f Initialization multiplier.
  413. */
  414. template<typename _UIntType, size_t __w,
  415. size_t __n, size_t __m, size_t __r,
  416. _UIntType __a, size_t __u, _UIntType __d, size_t __s,
  417. _UIntType __b, size_t __t,
  418. _UIntType __c, size_t __l, _UIntType __f>
  419. class mersenne_twister_engine
  420. {
  421. static_assert(std::is_unsigned<_UIntType>::value,
  422. "result_type must be an unsigned integral type");
  423. static_assert(1u <= __m && __m <= __n,
  424. "template argument substituting __m out of bounds");
  425. static_assert(__r <= __w, "template argument substituting "
  426. "__r out of bound");
  427. static_assert(__u <= __w, "template argument substituting "
  428. "__u out of bound");
  429. static_assert(__s <= __w, "template argument substituting "
  430. "__s out of bound");
  431. static_assert(__t <= __w, "template argument substituting "
  432. "__t out of bound");
  433. static_assert(__l <= __w, "template argument substituting "
  434. "__l out of bound");
  435. static_assert(__w <= std::numeric_limits<_UIntType>::digits,
  436. "template argument substituting __w out of bound");
  437. static_assert(__a <= (__detail::_Shift<_UIntType, __w>::__value - 1),
  438. "template argument substituting __a out of bound");
  439. static_assert(__b <= (__detail::_Shift<_UIntType, __w>::__value - 1),
  440. "template argument substituting __b out of bound");
  441. static_assert(__c <= (__detail::_Shift<_UIntType, __w>::__value - 1),
  442. "template argument substituting __c out of bound");
  443. static_assert(__d <= (__detail::_Shift<_UIntType, __w>::__value - 1),
  444. "template argument substituting __d out of bound");
  445. static_assert(__f <= (__detail::_Shift<_UIntType, __w>::__value - 1),
  446. "template argument substituting __f out of bound");
  447. template<typename _Sseq>
  448. using _If_seed_seq = typename enable_if<__detail::__is_seed_seq<
  449. _Sseq, mersenne_twister_engine, _UIntType>::value>::type;
  450. public:
  451. /** The type of the generated random value. */
  452. typedef _UIntType result_type;
  453. // parameter values
  454. static constexpr size_t word_size = __w;
  455. static constexpr size_t state_size = __n;
  456. static constexpr size_t shift_size = __m;
  457. static constexpr size_t mask_bits = __r;
  458. static constexpr result_type xor_mask = __a;
  459. static constexpr size_t tempering_u = __u;
  460. static constexpr result_type tempering_d = __d;
  461. static constexpr size_t tempering_s = __s;
  462. static constexpr result_type tempering_b = __b;
  463. static constexpr size_t tempering_t = __t;
  464. static constexpr result_type tempering_c = __c;
  465. static constexpr size_t tempering_l = __l;
  466. static constexpr result_type initialization_multiplier = __f;
  467. static constexpr result_type default_seed = 5489u;
  468. // constructors and member functions
  469. mersenne_twister_engine() : mersenne_twister_engine(default_seed) { }
  470. explicit
  471. mersenne_twister_engine(result_type __sd)
  472. { seed(__sd); }
  473. /**
  474. * @brief Constructs a %mersenne_twister_engine random number generator
  475. * engine seeded from the seed sequence @p __q.
  476. *
  477. * @param __q the seed sequence.
  478. */
  479. template<typename _Sseq, typename = _If_seed_seq<_Sseq>>
  480. explicit
  481. mersenne_twister_engine(_Sseq& __q)
  482. { seed(__q); }
  483. void
  484. seed(result_type __sd = default_seed);
  485. template<typename _Sseq>
  486. _If_seed_seq<_Sseq>
  487. seed(_Sseq& __q);
  488. /**
  489. * @brief Gets the smallest possible value in the output range.
  490. */
  491. static constexpr result_type
  492. min()
  493. { return 0; }
  494. /**
  495. * @brief Gets the largest possible value in the output range.
  496. */
  497. static constexpr result_type
  498. max()
  499. { return __detail::_Shift<_UIntType, __w>::__value - 1; }
  500. /**
  501. * @brief Discard a sequence of random numbers.
  502. */
  503. void
  504. discard(unsigned long long __z);
  505. result_type
  506. operator()();
  507. /**
  508. * @brief Compares two % mersenne_twister_engine random number generator
  509. * objects of the same type for equality.
  510. *
  511. * @param __lhs A % mersenne_twister_engine random number generator
  512. * object.
  513. * @param __rhs Another % mersenne_twister_engine random number
  514. * generator object.
  515. *
  516. * @returns true if the infinite sequences of generated values
  517. * would be equal, false otherwise.
  518. */
  519. friend bool
  520. operator==(const mersenne_twister_engine& __lhs,
  521. const mersenne_twister_engine& __rhs)
  522. { return (std::equal(__lhs._M_x, __lhs._M_x + state_size, __rhs._M_x)
  523. && __lhs._M_p == __rhs._M_p); }
  524. /**
  525. * @brief Inserts the current state of a % mersenne_twister_engine
  526. * random number generator engine @p __x into the output stream
  527. * @p __os.
  528. *
  529. * @param __os An output stream.
  530. * @param __x A % mersenne_twister_engine random number generator
  531. * engine.
  532. *
  533. * @returns The output stream with the state of @p __x inserted or in
  534. * an error state.
  535. */
  536. template<typename _UIntType1,
  537. size_t __w1, size_t __n1,
  538. size_t __m1, size_t __r1,
  539. _UIntType1 __a1, size_t __u1,
  540. _UIntType1 __d1, size_t __s1,
  541. _UIntType1 __b1, size_t __t1,
  542. _UIntType1 __c1, size_t __l1, _UIntType1 __f1,
  543. typename _CharT, typename _Traits>
  544. friend std::basic_ostream<_CharT, _Traits>&
  545. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  546. const std::mersenne_twister_engine<_UIntType1, __w1, __n1,
  547. __m1, __r1, __a1, __u1, __d1, __s1, __b1, __t1, __c1,
  548. __l1, __f1>& __x);
  549. /**
  550. * @brief Extracts the current state of a % mersenne_twister_engine
  551. * random number generator engine @p __x from the input stream
  552. * @p __is.
  553. *
  554. * @param __is An input stream.
  555. * @param __x A % mersenne_twister_engine random number generator
  556. * engine.
  557. *
  558. * @returns The input stream with the state of @p __x extracted or in
  559. * an error state.
  560. */
  561. template<typename _UIntType1,
  562. size_t __w1, size_t __n1,
  563. size_t __m1, size_t __r1,
  564. _UIntType1 __a1, size_t __u1,
  565. _UIntType1 __d1, size_t __s1,
  566. _UIntType1 __b1, size_t __t1,
  567. _UIntType1 __c1, size_t __l1, _UIntType1 __f1,
  568. typename _CharT, typename _Traits>
  569. friend std::basic_istream<_CharT, _Traits>&
  570. operator>>(std::basic_istream<_CharT, _Traits>& __is,
  571. std::mersenne_twister_engine<_UIntType1, __w1, __n1, __m1,
  572. __r1, __a1, __u1, __d1, __s1, __b1, __t1, __c1,
  573. __l1, __f1>& __x);
  574. private:
  575. void _M_gen_rand();
  576. _UIntType _M_x[state_size];
  577. size_t _M_p;
  578. };
  579. /**
  580. * @brief Compares two % mersenne_twister_engine random number generator
  581. * objects of the same type for inequality.
  582. *
  583. * @param __lhs A % mersenne_twister_engine random number generator
  584. * object.
  585. * @param __rhs Another % mersenne_twister_engine random number
  586. * generator object.
  587. *
  588. * @returns true if the infinite sequences of generated values
  589. * would be different, false otherwise.
  590. */
  591. template<typename _UIntType, size_t __w,
  592. size_t __n, size_t __m, size_t __r,
  593. _UIntType __a, size_t __u, _UIntType __d, size_t __s,
  594. _UIntType __b, size_t __t,
  595. _UIntType __c, size_t __l, _UIntType __f>
  596. inline bool
  597. operator!=(const std::mersenne_twister_engine<_UIntType, __w, __n, __m,
  598. __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>& __lhs,
  599. const std::mersenne_twister_engine<_UIntType, __w, __n, __m,
  600. __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>& __rhs)
  601. { return !(__lhs == __rhs); }
  602. /**
  603. * @brief The Marsaglia-Zaman generator.
  604. *
  605. * This is a model of a Generalized Fibonacci discrete random number
  606. * generator, sometimes referred to as the SWC generator.
  607. *
  608. * A discrete random number generator that produces pseudorandom
  609. * numbers using:
  610. * @f[
  611. * x_{i}\leftarrow(x_{i - s} - x_{i - r} - carry_{i-1}) \bmod m
  612. * @f]
  613. *
  614. * The size of the state is @f$r@f$
  615. * and the maximum period of the generator is @f$(m^r - m^s - 1)@f$.
  616. */
  617. template<typename _UIntType, size_t __w, size_t __s, size_t __r>
  618. class subtract_with_carry_engine
  619. {
  620. static_assert(std::is_unsigned<_UIntType>::value,
  621. "result_type must be an unsigned integral type");
  622. static_assert(0u < __s && __s < __r,
  623. "0 < s < r");
  624. static_assert(0u < __w && __w <= std::numeric_limits<_UIntType>::digits,
  625. "template argument substituting __w out of bounds");
  626. template<typename _Sseq>
  627. using _If_seed_seq = typename enable_if<__detail::__is_seed_seq<
  628. _Sseq, subtract_with_carry_engine, _UIntType>::value>::type;
  629. public:
  630. /** The type of the generated random value. */
  631. typedef _UIntType result_type;
  632. // parameter values
  633. static constexpr size_t word_size = __w;
  634. static constexpr size_t short_lag = __s;
  635. static constexpr size_t long_lag = __r;
  636. static constexpr result_type default_seed = 19780503u;
  637. subtract_with_carry_engine() : subtract_with_carry_engine(default_seed)
  638. { }
  639. /**
  640. * @brief Constructs an explicitly seeded %subtract_with_carry_engine
  641. * random number generator.
  642. */
  643. explicit
  644. subtract_with_carry_engine(result_type __sd)
  645. { seed(__sd); }
  646. /**
  647. * @brief Constructs a %subtract_with_carry_engine random number engine
  648. * seeded from the seed sequence @p __q.
  649. *
  650. * @param __q the seed sequence.
  651. */
  652. template<typename _Sseq, typename = _If_seed_seq<_Sseq>>
  653. explicit
  654. subtract_with_carry_engine(_Sseq& __q)
  655. { seed(__q); }
  656. /**
  657. * @brief Seeds the initial state @f$x_0@f$ of the random number
  658. * generator.
  659. *
  660. * N1688[4.19] modifies this as follows. If @p __value == 0,
  661. * sets value to 19780503. In any case, with a linear
  662. * congruential generator lcg(i) having parameters @f$ m_{lcg} =
  663. * 2147483563, a_{lcg} = 40014, c_{lcg} = 0, and lcg(0) = value
  664. * @f$, sets @f$ x_{-r} \dots x_{-1} @f$ to @f$ lcg(1) \bmod m
  665. * \dots lcg(r) \bmod m @f$ respectively. If @f$ x_{-1} = 0 @f$
  666. * set carry to 1, otherwise sets carry to 0.
  667. */
  668. void
  669. seed(result_type __sd = default_seed);
  670. /**
  671. * @brief Seeds the initial state @f$x_0@f$ of the
  672. * % subtract_with_carry_engine random number generator.
  673. */
  674. template<typename _Sseq>
  675. _If_seed_seq<_Sseq>
  676. seed(_Sseq& __q);
  677. /**
  678. * @brief Gets the inclusive minimum value of the range of random
  679. * integers returned by this generator.
  680. */
  681. static constexpr result_type
  682. min()
  683. { return 0; }
  684. /**
  685. * @brief Gets the inclusive maximum value of the range of random
  686. * integers returned by this generator.
  687. */
  688. static constexpr result_type
  689. max()
  690. { return __detail::_Shift<_UIntType, __w>::__value - 1; }
  691. /**
  692. * @brief Discard a sequence of random numbers.
  693. */
  694. void
  695. discard(unsigned long long __z)
  696. {
  697. for (; __z != 0ULL; --__z)
  698. (*this)();
  699. }
  700. /**
  701. * @brief Gets the next random number in the sequence.
  702. */
  703. result_type
  704. operator()();
  705. /**
  706. * @brief Compares two % subtract_with_carry_engine random number
  707. * generator objects of the same type for equality.
  708. *
  709. * @param __lhs A % subtract_with_carry_engine random number generator
  710. * object.
  711. * @param __rhs Another % subtract_with_carry_engine random number
  712. * generator object.
  713. *
  714. * @returns true if the infinite sequences of generated values
  715. * would be equal, false otherwise.
  716. */
  717. friend bool
  718. operator==(const subtract_with_carry_engine& __lhs,
  719. const subtract_with_carry_engine& __rhs)
  720. { return (std::equal(__lhs._M_x, __lhs._M_x + long_lag, __rhs._M_x)
  721. && __lhs._M_carry == __rhs._M_carry
  722. && __lhs._M_p == __rhs._M_p); }
  723. /**
  724. * @brief Inserts the current state of a % subtract_with_carry_engine
  725. * random number generator engine @p __x into the output stream
  726. * @p __os.
  727. *
  728. * @param __os An output stream.
  729. * @param __x A % subtract_with_carry_engine random number generator
  730. * engine.
  731. *
  732. * @returns The output stream with the state of @p __x inserted or in
  733. * an error state.
  734. */
  735. template<typename _UIntType1, size_t __w1, size_t __s1, size_t __r1,
  736. typename _CharT, typename _Traits>
  737. friend std::basic_ostream<_CharT, _Traits>&
  738. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  739. const std::subtract_with_carry_engine<_UIntType1, __w1,
  740. __s1, __r1>& __x);
  741. /**
  742. * @brief Extracts the current state of a % subtract_with_carry_engine
  743. * random number generator engine @p __x from the input stream
  744. * @p __is.
  745. *
  746. * @param __is An input stream.
  747. * @param __x A % subtract_with_carry_engine random number generator
  748. * engine.
  749. *
  750. * @returns The input stream with the state of @p __x extracted or in
  751. * an error state.
  752. */
  753. template<typename _UIntType1, size_t __w1, size_t __s1, size_t __r1,
  754. typename _CharT, typename _Traits>
  755. friend std::basic_istream<_CharT, _Traits>&
  756. operator>>(std::basic_istream<_CharT, _Traits>& __is,
  757. std::subtract_with_carry_engine<_UIntType1, __w1,
  758. __s1, __r1>& __x);
  759. private:
  760. /// The state of the generator. This is a ring buffer.
  761. _UIntType _M_x[long_lag];
  762. _UIntType _M_carry; ///< The carry
  763. size_t _M_p; ///< Current index of x(i - r).
  764. };
  765. /**
  766. * @brief Compares two % subtract_with_carry_engine random number
  767. * generator objects of the same type for inequality.
  768. *
  769. * @param __lhs A % subtract_with_carry_engine random number generator
  770. * object.
  771. * @param __rhs Another % subtract_with_carry_engine random number
  772. * generator object.
  773. *
  774. * @returns true if the infinite sequences of generated values
  775. * would be different, false otherwise.
  776. */
  777. template<typename _UIntType, size_t __w, size_t __s, size_t __r>
  778. inline bool
  779. operator!=(const std::subtract_with_carry_engine<_UIntType, __w,
  780. __s, __r>& __lhs,
  781. const std::subtract_with_carry_engine<_UIntType, __w,
  782. __s, __r>& __rhs)
  783. { return !(__lhs == __rhs); }
  784. /**
  785. * Produces random numbers from some base engine by discarding blocks of
  786. * data.
  787. *
  788. * 0 <= @p __r <= @p __p
  789. */
  790. template<typename _RandomNumberEngine, size_t __p, size_t __r>
  791. class discard_block_engine
  792. {
  793. static_assert(1 <= __r && __r <= __p,
  794. "template argument substituting __r out of bounds");
  795. public:
  796. /** The type of the generated random value. */
  797. typedef typename _RandomNumberEngine::result_type result_type;
  798. template<typename _Sseq>
  799. using _If_seed_seq = typename enable_if<__detail::__is_seed_seq<
  800. _Sseq, discard_block_engine, result_type>::value>::type;
  801. // parameter values
  802. static constexpr size_t block_size = __p;
  803. static constexpr size_t used_block = __r;
  804. /**
  805. * @brief Constructs a default %discard_block_engine engine.
  806. *
  807. * The underlying engine is default constructed as well.
  808. */
  809. discard_block_engine()
  810. : _M_b(), _M_n(0) { }
  811. /**
  812. * @brief Copy constructs a %discard_block_engine engine.
  813. *
  814. * Copies an existing base class random number generator.
  815. * @param __rng An existing (base class) engine object.
  816. */
  817. explicit
  818. discard_block_engine(const _RandomNumberEngine& __rng)
  819. : _M_b(__rng), _M_n(0) { }
  820. /**
  821. * @brief Move constructs a %discard_block_engine engine.
  822. *
  823. * Copies an existing base class random number generator.
  824. * @param __rng An existing (base class) engine object.
  825. */
  826. explicit
  827. discard_block_engine(_RandomNumberEngine&& __rng)
  828. : _M_b(std::move(__rng)), _M_n(0) { }
  829. /**
  830. * @brief Seed constructs a %discard_block_engine engine.
  831. *
  832. * Constructs the underlying generator engine seeded with @p __s.
  833. * @param __s A seed value for the base class engine.
  834. */
  835. explicit
  836. discard_block_engine(result_type __s)
  837. : _M_b(__s), _M_n(0) { }
  838. /**
  839. * @brief Generator construct a %discard_block_engine engine.
  840. *
  841. * @param __q A seed sequence.
  842. */
  843. template<typename _Sseq, typename = _If_seed_seq<_Sseq>>
  844. explicit
  845. discard_block_engine(_Sseq& __q)
  846. : _M_b(__q), _M_n(0)
  847. { }
  848. /**
  849. * @brief Reseeds the %discard_block_engine object with the default
  850. * seed for the underlying base class generator engine.
  851. */
  852. void
  853. seed()
  854. {
  855. _M_b.seed();
  856. _M_n = 0;
  857. }
  858. /**
  859. * @brief Reseeds the %discard_block_engine object with the default
  860. * seed for the underlying base class generator engine.
  861. */
  862. void
  863. seed(result_type __s)
  864. {
  865. _M_b.seed(__s);
  866. _M_n = 0;
  867. }
  868. /**
  869. * @brief Reseeds the %discard_block_engine object with the given seed
  870. * sequence.
  871. * @param __q A seed generator function.
  872. */
  873. template<typename _Sseq>
  874. _If_seed_seq<_Sseq>
  875. seed(_Sseq& __q)
  876. {
  877. _M_b.seed(__q);
  878. _M_n = 0;
  879. }
  880. /**
  881. * @brief Gets a const reference to the underlying generator engine
  882. * object.
  883. */
  884. const _RandomNumberEngine&
  885. base() const noexcept
  886. { return _M_b; }
  887. /**
  888. * @brief Gets the minimum value in the generated random number range.
  889. */
  890. static constexpr result_type
  891. min()
  892. { return _RandomNumberEngine::min(); }
  893. /**
  894. * @brief Gets the maximum value in the generated random number range.
  895. */
  896. static constexpr result_type
  897. max()
  898. { return _RandomNumberEngine::max(); }
  899. /**
  900. * @brief Discard a sequence of random numbers.
  901. */
  902. void
  903. discard(unsigned long long __z)
  904. {
  905. for (; __z != 0ULL; --__z)
  906. (*this)();
  907. }
  908. /**
  909. * @brief Gets the next value in the generated random number sequence.
  910. */
  911. result_type
  912. operator()();
  913. /**
  914. * @brief Compares two %discard_block_engine random number generator
  915. * objects of the same type for equality.
  916. *
  917. * @param __lhs A %discard_block_engine random number generator object.
  918. * @param __rhs Another %discard_block_engine random number generator
  919. * object.
  920. *
  921. * @returns true if the infinite sequences of generated values
  922. * would be equal, false otherwise.
  923. */
  924. friend bool
  925. operator==(const discard_block_engine& __lhs,
  926. const discard_block_engine& __rhs)
  927. { return __lhs._M_b == __rhs._M_b && __lhs._M_n == __rhs._M_n; }
  928. /**
  929. * @brief Inserts the current state of a %discard_block_engine random
  930. * number generator engine @p __x into the output stream
  931. * @p __os.
  932. *
  933. * @param __os An output stream.
  934. * @param __x A %discard_block_engine random number generator engine.
  935. *
  936. * @returns The output stream with the state of @p __x inserted or in
  937. * an error state.
  938. */
  939. template<typename _RandomNumberEngine1, size_t __p1, size_t __r1,
  940. typename _CharT, typename _Traits>
  941. friend std::basic_ostream<_CharT, _Traits>&
  942. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  943. const std::discard_block_engine<_RandomNumberEngine1,
  944. __p1, __r1>& __x);
  945. /**
  946. * @brief Extracts the current state of a % subtract_with_carry_engine
  947. * random number generator engine @p __x from the input stream
  948. * @p __is.
  949. *
  950. * @param __is An input stream.
  951. * @param __x A %discard_block_engine random number generator engine.
  952. *
  953. * @returns The input stream with the state of @p __x extracted or in
  954. * an error state.
  955. */
  956. template<typename _RandomNumberEngine1, size_t __p1, size_t __r1,
  957. typename _CharT, typename _Traits>
  958. friend std::basic_istream<_CharT, _Traits>&
  959. operator>>(std::basic_istream<_CharT, _Traits>& __is,
  960. std::discard_block_engine<_RandomNumberEngine1,
  961. __p1, __r1>& __x);
  962. private:
  963. _RandomNumberEngine _M_b;
  964. size_t _M_n;
  965. };
  966. /**
  967. * @brief Compares two %discard_block_engine random number generator
  968. * objects of the same type for inequality.
  969. *
  970. * @param __lhs A %discard_block_engine random number generator object.
  971. * @param __rhs Another %discard_block_engine random number generator
  972. * object.
  973. *
  974. * @returns true if the infinite sequences of generated values
  975. * would be different, false otherwise.
  976. */
  977. template<typename _RandomNumberEngine, size_t __p, size_t __r>
  978. inline bool
  979. operator!=(const std::discard_block_engine<_RandomNumberEngine, __p,
  980. __r>& __lhs,
  981. const std::discard_block_engine<_RandomNumberEngine, __p,
  982. __r>& __rhs)
  983. { return !(__lhs == __rhs); }
  984. /**
  985. * Produces random numbers by combining random numbers from some base
  986. * engine to produce random numbers with a specified number of bits @p __w.
  987. */
  988. template<typename _RandomNumberEngine, size_t __w, typename _UIntType>
  989. class independent_bits_engine
  990. {
  991. static_assert(std::is_unsigned<_UIntType>::value,
  992. "result_type must be an unsigned integral type");
  993. static_assert(0u < __w && __w <= std::numeric_limits<_UIntType>::digits,
  994. "template argument substituting __w out of bounds");
  995. template<typename _Sseq>
  996. using _If_seed_seq = typename enable_if<__detail::__is_seed_seq<
  997. _Sseq, independent_bits_engine, _UIntType>::value>::type;
  998. public:
  999. /** The type of the generated random value. */
  1000. typedef _UIntType result_type;
  1001. /**
  1002. * @brief Constructs a default %independent_bits_engine engine.
  1003. *
  1004. * The underlying engine is default constructed as well.
  1005. */
  1006. independent_bits_engine()
  1007. : _M_b() { }
  1008. /**
  1009. * @brief Copy constructs a %independent_bits_engine engine.
  1010. *
  1011. * Copies an existing base class random number generator.
  1012. * @param __rng An existing (base class) engine object.
  1013. */
  1014. explicit
  1015. independent_bits_engine(const _RandomNumberEngine& __rng)
  1016. : _M_b(__rng) { }
  1017. /**
  1018. * @brief Move constructs a %independent_bits_engine engine.
  1019. *
  1020. * Copies an existing base class random number generator.
  1021. * @param __rng An existing (base class) engine object.
  1022. */
  1023. explicit
  1024. independent_bits_engine(_RandomNumberEngine&& __rng)
  1025. : _M_b(std::move(__rng)) { }
  1026. /**
  1027. * @brief Seed constructs a %independent_bits_engine engine.
  1028. *
  1029. * Constructs the underlying generator engine seeded with @p __s.
  1030. * @param __s A seed value for the base class engine.
  1031. */
  1032. explicit
  1033. independent_bits_engine(result_type __s)
  1034. : _M_b(__s) { }
  1035. /**
  1036. * @brief Generator construct a %independent_bits_engine engine.
  1037. *
  1038. * @param __q A seed sequence.
  1039. */
  1040. template<typename _Sseq, typename = _If_seed_seq<_Sseq>>
  1041. explicit
  1042. independent_bits_engine(_Sseq& __q)
  1043. : _M_b(__q)
  1044. { }
  1045. /**
  1046. * @brief Reseeds the %independent_bits_engine object with the default
  1047. * seed for the underlying base class generator engine.
  1048. */
  1049. void
  1050. seed()
  1051. { _M_b.seed(); }
  1052. /**
  1053. * @brief Reseeds the %independent_bits_engine object with the default
  1054. * seed for the underlying base class generator engine.
  1055. */
  1056. void
  1057. seed(result_type __s)
  1058. { _M_b.seed(__s); }
  1059. /**
  1060. * @brief Reseeds the %independent_bits_engine object with the given
  1061. * seed sequence.
  1062. * @param __q A seed generator function.
  1063. */
  1064. template<typename _Sseq>
  1065. _If_seed_seq<_Sseq>
  1066. seed(_Sseq& __q)
  1067. { _M_b.seed(__q); }
  1068. /**
  1069. * @brief Gets a const reference to the underlying generator engine
  1070. * object.
  1071. */
  1072. const _RandomNumberEngine&
  1073. base() const noexcept
  1074. { return _M_b; }
  1075. /**
  1076. * @brief Gets the minimum value in the generated random number range.
  1077. */
  1078. static constexpr result_type
  1079. min()
  1080. { return 0U; }
  1081. /**
  1082. * @brief Gets the maximum value in the generated random number range.
  1083. */
  1084. static constexpr result_type
  1085. max()
  1086. { return __detail::_Shift<_UIntType, __w>::__value - 1; }
  1087. /**
  1088. * @brief Discard a sequence of random numbers.
  1089. */
  1090. void
  1091. discard(unsigned long long __z)
  1092. {
  1093. for (; __z != 0ULL; --__z)
  1094. (*this)();
  1095. }
  1096. /**
  1097. * @brief Gets the next value in the generated random number sequence.
  1098. */
  1099. result_type
  1100. operator()();
  1101. /**
  1102. * @brief Compares two %independent_bits_engine random number generator
  1103. * objects of the same type for equality.
  1104. *
  1105. * @param __lhs A %independent_bits_engine random number generator
  1106. * object.
  1107. * @param __rhs Another %independent_bits_engine random number generator
  1108. * object.
  1109. *
  1110. * @returns true if the infinite sequences of generated values
  1111. * would be equal, false otherwise.
  1112. */
  1113. friend bool
  1114. operator==(const independent_bits_engine& __lhs,
  1115. const independent_bits_engine& __rhs)
  1116. { return __lhs._M_b == __rhs._M_b; }
  1117. /**
  1118. * @brief Extracts the current state of a % subtract_with_carry_engine
  1119. * random number generator engine @p __x from the input stream
  1120. * @p __is.
  1121. *
  1122. * @param __is An input stream.
  1123. * @param __x A %independent_bits_engine random number generator
  1124. * engine.
  1125. *
  1126. * @returns The input stream with the state of @p __x extracted or in
  1127. * an error state.
  1128. */
  1129. template<typename _CharT, typename _Traits>
  1130. friend std::basic_istream<_CharT, _Traits>&
  1131. operator>>(std::basic_istream<_CharT, _Traits>& __is,
  1132. std::independent_bits_engine<_RandomNumberEngine,
  1133. __w, _UIntType>& __x)
  1134. {
  1135. __is >> __x._M_b;
  1136. return __is;
  1137. }
  1138. private:
  1139. _RandomNumberEngine _M_b;
  1140. };
  1141. /**
  1142. * @brief Compares two %independent_bits_engine random number generator
  1143. * objects of the same type for inequality.
  1144. *
  1145. * @param __lhs A %independent_bits_engine random number generator
  1146. * object.
  1147. * @param __rhs Another %independent_bits_engine random number generator
  1148. * object.
  1149. *
  1150. * @returns true if the infinite sequences of generated values
  1151. * would be different, false otherwise.
  1152. */
  1153. template<typename _RandomNumberEngine, size_t __w, typename _UIntType>
  1154. inline bool
  1155. operator!=(const std::independent_bits_engine<_RandomNumberEngine, __w,
  1156. _UIntType>& __lhs,
  1157. const std::independent_bits_engine<_RandomNumberEngine, __w,
  1158. _UIntType>& __rhs)
  1159. { return !(__lhs == __rhs); }
  1160. /**
  1161. * @brief Inserts the current state of a %independent_bits_engine random
  1162. * number generator engine @p __x into the output stream @p __os.
  1163. *
  1164. * @param __os An output stream.
  1165. * @param __x A %independent_bits_engine random number generator engine.
  1166. *
  1167. * @returns The output stream with the state of @p __x inserted or in
  1168. * an error state.
  1169. */
  1170. template<typename _RandomNumberEngine, size_t __w, typename _UIntType,
  1171. typename _CharT, typename _Traits>
  1172. std::basic_ostream<_CharT, _Traits>&
  1173. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  1174. const std::independent_bits_engine<_RandomNumberEngine,
  1175. __w, _UIntType>& __x)
  1176. {
  1177. __os << __x.base();
  1178. return __os;
  1179. }
  1180. /**
  1181. * @brief Produces random numbers by reordering random numbers from some
  1182. * base engine.
  1183. *
  1184. * The values from the base engine are stored in a sequence of size @p __k
  1185. * and shuffled by an algorithm that depends on those values.
  1186. */
  1187. template<typename _RandomNumberEngine, size_t __k>
  1188. class shuffle_order_engine
  1189. {
  1190. static_assert(1u <= __k, "template argument substituting "
  1191. "__k out of bound");
  1192. public:
  1193. /** The type of the generated random value. */
  1194. typedef typename _RandomNumberEngine::result_type result_type;
  1195. template<typename _Sseq>
  1196. using _If_seed_seq = typename enable_if<__detail::__is_seed_seq<
  1197. _Sseq, shuffle_order_engine, result_type>::value>::type;
  1198. static constexpr size_t table_size = __k;
  1199. /**
  1200. * @brief Constructs a default %shuffle_order_engine engine.
  1201. *
  1202. * The underlying engine is default constructed as well.
  1203. */
  1204. shuffle_order_engine()
  1205. : _M_b()
  1206. { _M_initialize(); }
  1207. /**
  1208. * @brief Copy constructs a %shuffle_order_engine engine.
  1209. *
  1210. * Copies an existing base class random number generator.
  1211. * @param __rng An existing (base class) engine object.
  1212. */
  1213. explicit
  1214. shuffle_order_engine(const _RandomNumberEngine& __rng)
  1215. : _M_b(__rng)
  1216. { _M_initialize(); }
  1217. /**
  1218. * @brief Move constructs a %shuffle_order_engine engine.
  1219. *
  1220. * Copies an existing base class random number generator.
  1221. * @param __rng An existing (base class) engine object.
  1222. */
  1223. explicit
  1224. shuffle_order_engine(_RandomNumberEngine&& __rng)
  1225. : _M_b(std::move(__rng))
  1226. { _M_initialize(); }
  1227. /**
  1228. * @brief Seed constructs a %shuffle_order_engine engine.
  1229. *
  1230. * Constructs the underlying generator engine seeded with @p __s.
  1231. * @param __s A seed value for the base class engine.
  1232. */
  1233. explicit
  1234. shuffle_order_engine(result_type __s)
  1235. : _M_b(__s)
  1236. { _M_initialize(); }
  1237. /**
  1238. * @brief Generator construct a %shuffle_order_engine engine.
  1239. *
  1240. * @param __q A seed sequence.
  1241. */
  1242. template<typename _Sseq, typename = _If_seed_seq<_Sseq>>
  1243. explicit
  1244. shuffle_order_engine(_Sseq& __q)
  1245. : _M_b(__q)
  1246. { _M_initialize(); }
  1247. /**
  1248. * @brief Reseeds the %shuffle_order_engine object with the default seed
  1249. for the underlying base class generator engine.
  1250. */
  1251. void
  1252. seed()
  1253. {
  1254. _M_b.seed();
  1255. _M_initialize();
  1256. }
  1257. /**
  1258. * @brief Reseeds the %shuffle_order_engine object with the default seed
  1259. * for the underlying base class generator engine.
  1260. */
  1261. void
  1262. seed(result_type __s)
  1263. {
  1264. _M_b.seed(__s);
  1265. _M_initialize();
  1266. }
  1267. /**
  1268. * @brief Reseeds the %shuffle_order_engine object with the given seed
  1269. * sequence.
  1270. * @param __q A seed generator function.
  1271. */
  1272. template<typename _Sseq>
  1273. _If_seed_seq<_Sseq>
  1274. seed(_Sseq& __q)
  1275. {
  1276. _M_b.seed(__q);
  1277. _M_initialize();
  1278. }
  1279. /**
  1280. * Gets a const reference to the underlying generator engine object.
  1281. */
  1282. const _RandomNumberEngine&
  1283. base() const noexcept
  1284. { return _M_b; }
  1285. /**
  1286. * Gets the minimum value in the generated random number range.
  1287. */
  1288. static constexpr result_type
  1289. min()
  1290. { return _RandomNumberEngine::min(); }
  1291. /**
  1292. * Gets the maximum value in the generated random number range.
  1293. */
  1294. static constexpr result_type
  1295. max()
  1296. { return _RandomNumberEngine::max(); }
  1297. /**
  1298. * Discard a sequence of random numbers.
  1299. */
  1300. void
  1301. discard(unsigned long long __z)
  1302. {
  1303. for (; __z != 0ULL; --__z)
  1304. (*this)();
  1305. }
  1306. /**
  1307. * Gets the next value in the generated random number sequence.
  1308. */
  1309. result_type
  1310. operator()();
  1311. /**
  1312. * Compares two %shuffle_order_engine random number generator objects
  1313. * of the same type for equality.
  1314. *
  1315. * @param __lhs A %shuffle_order_engine random number generator object.
  1316. * @param __rhs Another %shuffle_order_engine random number generator
  1317. * object.
  1318. *
  1319. * @returns true if the infinite sequences of generated values
  1320. * would be equal, false otherwise.
  1321. */
  1322. friend bool
  1323. operator==(const shuffle_order_engine& __lhs,
  1324. const shuffle_order_engine& __rhs)
  1325. { return (__lhs._M_b == __rhs._M_b
  1326. && std::equal(__lhs._M_v, __lhs._M_v + __k, __rhs._M_v)
  1327. && __lhs._M_y == __rhs._M_y); }
  1328. /**
  1329. * @brief Inserts the current state of a %shuffle_order_engine random
  1330. * number generator engine @p __x into the output stream
  1331. @p __os.
  1332. *
  1333. * @param __os An output stream.
  1334. * @param __x A %shuffle_order_engine random number generator engine.
  1335. *
  1336. * @returns The output stream with the state of @p __x inserted or in
  1337. * an error state.
  1338. */
  1339. template<typename _RandomNumberEngine1, size_t __k1,
  1340. typename _CharT, typename _Traits>
  1341. friend std::basic_ostream<_CharT, _Traits>&
  1342. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  1343. const std::shuffle_order_engine<_RandomNumberEngine1,
  1344. __k1>& __x);
  1345. /**
  1346. * @brief Extracts the current state of a % subtract_with_carry_engine
  1347. * random number generator engine @p __x from the input stream
  1348. * @p __is.
  1349. *
  1350. * @param __is An input stream.
  1351. * @param __x A %shuffle_order_engine random number generator engine.
  1352. *
  1353. * @returns The input stream with the state of @p __x extracted or in
  1354. * an error state.
  1355. */
  1356. template<typename _RandomNumberEngine1, size_t __k1,
  1357. typename _CharT, typename _Traits>
  1358. friend std::basic_istream<_CharT, _Traits>&
  1359. operator>>(std::basic_istream<_CharT, _Traits>& __is,
  1360. std::shuffle_order_engine<_RandomNumberEngine1, __k1>& __x);
  1361. private:
  1362. void _M_initialize()
  1363. {
  1364. for (size_t __i = 0; __i < __k; ++__i)
  1365. _M_v[__i] = _M_b();
  1366. _M_y = _M_b();
  1367. }
  1368. _RandomNumberEngine _M_b;
  1369. result_type _M_v[__k];
  1370. result_type _M_y;
  1371. };
  1372. /**
  1373. * Compares two %shuffle_order_engine random number generator objects
  1374. * of the same type for inequality.
  1375. *
  1376. * @param __lhs A %shuffle_order_engine random number generator object.
  1377. * @param __rhs Another %shuffle_order_engine random number generator
  1378. * object.
  1379. *
  1380. * @returns true if the infinite sequences of generated values
  1381. * would be different, false otherwise.
  1382. */
  1383. template<typename _RandomNumberEngine, size_t __k>
  1384. inline bool
  1385. operator!=(const std::shuffle_order_engine<_RandomNumberEngine,
  1386. __k>& __lhs,
  1387. const std::shuffle_order_engine<_RandomNumberEngine,
  1388. __k>& __rhs)
  1389. { return !(__lhs == __rhs); }
  1390. /**
  1391. * The classic Minimum Standard rand0 of Lewis, Goodman, and Miller.
  1392. */
  1393. typedef linear_congruential_engine<uint_fast32_t, 16807UL, 0UL, 2147483647UL>
  1394. minstd_rand0;
  1395. /**
  1396. * An alternative LCR (Lehmer Generator function).
  1397. */
  1398. typedef linear_congruential_engine<uint_fast32_t, 48271UL, 0UL, 2147483647UL>
  1399. minstd_rand;
  1400. /**
  1401. * The classic Mersenne Twister.
  1402. *
  1403. * Reference:
  1404. * M. Matsumoto and T. Nishimura, Mersenne Twister: A 623-Dimensionally
  1405. * Equidistributed Uniform Pseudo-Random Number Generator, ACM Transactions
  1406. * on Modeling and Computer Simulation, Vol. 8, No. 1, January 1998, pp 3-30.
  1407. */
  1408. typedef mersenne_twister_engine<
  1409. uint_fast32_t,
  1410. 32, 624, 397, 31,
  1411. 0x9908b0dfUL, 11,
  1412. 0xffffffffUL, 7,
  1413. 0x9d2c5680UL, 15,
  1414. 0xefc60000UL, 18, 1812433253UL> mt19937;
  1415. /**
  1416. * An alternative Mersenne Twister.
  1417. */
  1418. typedef mersenne_twister_engine<
  1419. uint_fast64_t,
  1420. 64, 312, 156, 31,
  1421. 0xb5026f5aa96619e9ULL, 29,
  1422. 0x5555555555555555ULL, 17,
  1423. 0x71d67fffeda60000ULL, 37,
  1424. 0xfff7eee000000000ULL, 43,
  1425. 6364136223846793005ULL> mt19937_64;
  1426. typedef subtract_with_carry_engine<uint_fast32_t, 24, 10, 24>
  1427. ranlux24_base;
  1428. typedef subtract_with_carry_engine<uint_fast64_t, 48, 5, 12>
  1429. ranlux48_base;
  1430. typedef discard_block_engine<ranlux24_base, 223, 23> ranlux24;
  1431. typedef discard_block_engine<ranlux48_base, 389, 11> ranlux48;
  1432. typedef shuffle_order_engine<minstd_rand0, 256> knuth_b;
  1433. typedef minstd_rand0 default_random_engine;
  1434. /**
  1435. * A standard interface to a platform-specific non-deterministic
  1436. * random number generator (if any are available).
  1437. */
  1438. class random_device
  1439. {
  1440. public:
  1441. /** The type of the generated random value. */
  1442. typedef unsigned int result_type;
  1443. // constructors, destructors and member functions
  1444. random_device() { _M_init("default"); }
  1445. explicit
  1446. random_device(const std::string& __token) { _M_init(__token); }
  1447. #if defined _GLIBCXX_USE_DEV_RANDOM
  1448. ~random_device()
  1449. { _M_fini(); }
  1450. #endif
  1451. static constexpr result_type
  1452. min()
  1453. { return std::numeric_limits<result_type>::min(); }
  1454. static constexpr result_type
  1455. max()
  1456. { return std::numeric_limits<result_type>::max(); }
  1457. double
  1458. entropy() const noexcept
  1459. {
  1460. #ifdef _GLIBCXX_USE_DEV_RANDOM
  1461. return this->_M_getentropy();
  1462. #else
  1463. return 0.0;
  1464. #endif
  1465. }
  1466. result_type
  1467. operator()()
  1468. { return this->_M_getval(); }
  1469. // No copy functions.
  1470. random_device(const random_device&) = delete;
  1471. void operator=(const random_device&) = delete;
  1472. private:
  1473. void _M_init(const std::string& __token);
  1474. void _M_init_pretr1(const std::string& __token);
  1475. void _M_fini();
  1476. result_type _M_getval();
  1477. result_type _M_getval_pretr1();
  1478. double _M_getentropy() const noexcept;
  1479. void _M_init(const char*, size_t); // not exported from the shared library
  1480. union
  1481. {
  1482. struct
  1483. {
  1484. void* _M_file;
  1485. result_type (*_M_func)(void*);
  1486. int _M_fd;
  1487. };
  1488. mt19937 _M_mt;
  1489. };
  1490. };
  1491. /// @} group random_generators
  1492. /**
  1493. * @addtogroup random_distributions Random Number Distributions
  1494. * @ingroup random
  1495. * @{
  1496. */
  1497. /**
  1498. * @addtogroup random_distributions_uniform Uniform Distributions
  1499. * @ingroup random_distributions
  1500. * @{
  1501. */
  1502. // std::uniform_int_distribution is defined in <bits/uniform_int_dist.h>
  1503. /**
  1504. * @brief Return true if two uniform integer distributions have
  1505. * different parameters.
  1506. */
  1507. template<typename _IntType>
  1508. inline bool
  1509. operator!=(const std::uniform_int_distribution<_IntType>& __d1,
  1510. const std::uniform_int_distribution<_IntType>& __d2)
  1511. { return !(__d1 == __d2); }
  1512. /**
  1513. * @brief Inserts a %uniform_int_distribution random number
  1514. * distribution @p __x into the output stream @p os.
  1515. *
  1516. * @param __os An output stream.
  1517. * @param __x A %uniform_int_distribution random number distribution.
  1518. *
  1519. * @returns The output stream with the state of @p __x inserted or in
  1520. * an error state.
  1521. */
  1522. template<typename _IntType, typename _CharT, typename _Traits>
  1523. std::basic_ostream<_CharT, _Traits>&
  1524. operator<<(std::basic_ostream<_CharT, _Traits>&,
  1525. const std::uniform_int_distribution<_IntType>&);
  1526. /**
  1527. * @brief Extracts a %uniform_int_distribution random number distribution
  1528. * @p __x from the input stream @p __is.
  1529. *
  1530. * @param __is An input stream.
  1531. * @param __x A %uniform_int_distribution random number generator engine.
  1532. *
  1533. * @returns The input stream with @p __x extracted or in an error state.
  1534. */
  1535. template<typename _IntType, typename _CharT, typename _Traits>
  1536. std::basic_istream<_CharT, _Traits>&
  1537. operator>>(std::basic_istream<_CharT, _Traits>&,
  1538. std::uniform_int_distribution<_IntType>&);
  1539. /**
  1540. * @brief Uniform continuous distribution for random numbers.
  1541. *
  1542. * A continuous random distribution on the range [min, max) with equal
  1543. * probability throughout the range. The URNG should be real-valued and
  1544. * deliver number in the range [0, 1).
  1545. */
  1546. template<typename _RealType = double>
  1547. class uniform_real_distribution
  1548. {
  1549. static_assert(std::is_floating_point<_RealType>::value,
  1550. "result_type must be a floating point type");
  1551. public:
  1552. /** The type of the range of the distribution. */
  1553. typedef _RealType result_type;
  1554. /** Parameter type. */
  1555. struct param_type
  1556. {
  1557. typedef uniform_real_distribution<_RealType> distribution_type;
  1558. param_type() : param_type(0) { }
  1559. explicit
  1560. param_type(_RealType __a, _RealType __b = _RealType(1))
  1561. : _M_a(__a), _M_b(__b)
  1562. {
  1563. __glibcxx_assert(_M_a <= _M_b);
  1564. }
  1565. result_type
  1566. a() const
  1567. { return _M_a; }
  1568. result_type
  1569. b() const
  1570. { return _M_b; }
  1571. friend bool
  1572. operator==(const param_type& __p1, const param_type& __p2)
  1573. { return __p1._M_a == __p2._M_a && __p1._M_b == __p2._M_b; }
  1574. friend bool
  1575. operator!=(const param_type& __p1, const param_type& __p2)
  1576. { return !(__p1 == __p2); }
  1577. private:
  1578. _RealType _M_a;
  1579. _RealType _M_b;
  1580. };
  1581. public:
  1582. /**
  1583. * @brief Constructs a uniform_real_distribution object.
  1584. *
  1585. * The lower bound is set to 0.0 and the upper bound to 1.0
  1586. */
  1587. uniform_real_distribution() : uniform_real_distribution(0.0) { }
  1588. /**
  1589. * @brief Constructs a uniform_real_distribution object.
  1590. *
  1591. * @param __a [IN] The lower bound of the distribution.
  1592. * @param __b [IN] The upper bound of the distribution.
  1593. */
  1594. explicit
  1595. uniform_real_distribution(_RealType __a, _RealType __b = _RealType(1))
  1596. : _M_param(__a, __b)
  1597. { }
  1598. explicit
  1599. uniform_real_distribution(const param_type& __p)
  1600. : _M_param(__p)
  1601. { }
  1602. /**
  1603. * @brief Resets the distribution state.
  1604. *
  1605. * Does nothing for the uniform real distribution.
  1606. */
  1607. void
  1608. reset() { }
  1609. result_type
  1610. a() const
  1611. { return _M_param.a(); }
  1612. result_type
  1613. b() const
  1614. { return _M_param.b(); }
  1615. /**
  1616. * @brief Returns the parameter set of the distribution.
  1617. */
  1618. param_type
  1619. param() const
  1620. { return _M_param; }
  1621. /**
  1622. * @brief Sets the parameter set of the distribution.
  1623. * @param __param The new parameter set of the distribution.
  1624. */
  1625. void
  1626. param(const param_type& __param)
  1627. { _M_param = __param; }
  1628. /**
  1629. * @brief Returns the inclusive lower bound of the distribution range.
  1630. */
  1631. result_type
  1632. min() const
  1633. { return this->a(); }
  1634. /**
  1635. * @brief Returns the inclusive upper bound of the distribution range.
  1636. */
  1637. result_type
  1638. max() const
  1639. { return this->b(); }
  1640. /**
  1641. * @brief Generating functions.
  1642. */
  1643. template<typename _UniformRandomNumberGenerator>
  1644. result_type
  1645. operator()(_UniformRandomNumberGenerator& __urng)
  1646. { return this->operator()(__urng, _M_param); }
  1647. template<typename _UniformRandomNumberGenerator>
  1648. result_type
  1649. operator()(_UniformRandomNumberGenerator& __urng,
  1650. const param_type& __p)
  1651. {
  1652. __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
  1653. __aurng(__urng);
  1654. return (__aurng() * (__p.b() - __p.a())) + __p.a();
  1655. }
  1656. template<typename _ForwardIterator,
  1657. typename _UniformRandomNumberGenerator>
  1658. void
  1659. __generate(_ForwardIterator __f, _ForwardIterator __t,
  1660. _UniformRandomNumberGenerator& __urng)
  1661. { this->__generate(__f, __t, __urng, _M_param); }
  1662. template<typename _ForwardIterator,
  1663. typename _UniformRandomNumberGenerator>
  1664. void
  1665. __generate(_ForwardIterator __f, _ForwardIterator __t,
  1666. _UniformRandomNumberGenerator& __urng,
  1667. const param_type& __p)
  1668. { this->__generate_impl(__f, __t, __urng, __p); }
  1669. template<typename _UniformRandomNumberGenerator>
  1670. void
  1671. __generate(result_type* __f, result_type* __t,
  1672. _UniformRandomNumberGenerator& __urng,
  1673. const param_type& __p)
  1674. { this->__generate_impl(__f, __t, __urng, __p); }
  1675. /**
  1676. * @brief Return true if two uniform real distributions have
  1677. * the same parameters.
  1678. */
  1679. friend bool
  1680. operator==(const uniform_real_distribution& __d1,
  1681. const uniform_real_distribution& __d2)
  1682. { return __d1._M_param == __d2._M_param; }
  1683. private:
  1684. template<typename _ForwardIterator,
  1685. typename _UniformRandomNumberGenerator>
  1686. void
  1687. __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
  1688. _UniformRandomNumberGenerator& __urng,
  1689. const param_type& __p);
  1690. param_type _M_param;
  1691. };
  1692. /**
  1693. * @brief Return true if two uniform real distributions have
  1694. * different parameters.
  1695. */
  1696. template<typename _IntType>
  1697. inline bool
  1698. operator!=(const std::uniform_real_distribution<_IntType>& __d1,
  1699. const std::uniform_real_distribution<_IntType>& __d2)
  1700. { return !(__d1 == __d2); }
  1701. /**
  1702. * @brief Inserts a %uniform_real_distribution random number
  1703. * distribution @p __x into the output stream @p __os.
  1704. *
  1705. * @param __os An output stream.
  1706. * @param __x A %uniform_real_distribution random number distribution.
  1707. *
  1708. * @returns The output stream with the state of @p __x inserted or in
  1709. * an error state.
  1710. */
  1711. template<typename _RealType, typename _CharT, typename _Traits>
  1712. std::basic_ostream<_CharT, _Traits>&
  1713. operator<<(std::basic_ostream<_CharT, _Traits>&,
  1714. const std::uniform_real_distribution<_RealType>&);
  1715. /**
  1716. * @brief Extracts a %uniform_real_distribution random number distribution
  1717. * @p __x from the input stream @p __is.
  1718. *
  1719. * @param __is An input stream.
  1720. * @param __x A %uniform_real_distribution random number generator engine.
  1721. *
  1722. * @returns The input stream with @p __x extracted or in an error state.
  1723. */
  1724. template<typename _RealType, typename _CharT, typename _Traits>
  1725. std::basic_istream<_CharT, _Traits>&
  1726. operator>>(std::basic_istream<_CharT, _Traits>&,
  1727. std::uniform_real_distribution<_RealType>&);
  1728. /// @} group random_distributions_uniform
  1729. /**
  1730. * @addtogroup random_distributions_normal Normal Distributions
  1731. * @ingroup random_distributions
  1732. * @{
  1733. */
  1734. /**
  1735. * @brief A normal continuous distribution for random numbers.
  1736. *
  1737. * The formula for the normal probability density function is
  1738. * @f[
  1739. * p(x|\mu,\sigma) = \frac{1}{\sigma \sqrt{2 \pi}}
  1740. * e^{- \frac{{x - \mu}^ {2}}{2 \sigma ^ {2}} }
  1741. * @f]
  1742. */
  1743. template<typename _RealType = double>
  1744. class normal_distribution
  1745. {
  1746. static_assert(std::is_floating_point<_RealType>::value,
  1747. "result_type must be a floating point type");
  1748. public:
  1749. /** The type of the range of the distribution. */
  1750. typedef _RealType result_type;
  1751. /** Parameter type. */
  1752. struct param_type
  1753. {
  1754. typedef normal_distribution<_RealType> distribution_type;
  1755. param_type() : param_type(0.0) { }
  1756. explicit
  1757. param_type(_RealType __mean, _RealType __stddev = _RealType(1))
  1758. : _M_mean(__mean), _M_stddev(__stddev)
  1759. {
  1760. __glibcxx_assert(_M_stddev > _RealType(0));
  1761. }
  1762. _RealType
  1763. mean() const
  1764. { return _M_mean; }
  1765. _RealType
  1766. stddev() const
  1767. { return _M_stddev; }
  1768. friend bool
  1769. operator==(const param_type& __p1, const param_type& __p2)
  1770. { return (__p1._M_mean == __p2._M_mean
  1771. && __p1._M_stddev == __p2._M_stddev); }
  1772. friend bool
  1773. operator!=(const param_type& __p1, const param_type& __p2)
  1774. { return !(__p1 == __p2); }
  1775. private:
  1776. _RealType _M_mean;
  1777. _RealType _M_stddev;
  1778. };
  1779. public:
  1780. normal_distribution() : normal_distribution(0.0) { }
  1781. /**
  1782. * Constructs a normal distribution with parameters @f$mean@f$ and
  1783. * standard deviation.
  1784. */
  1785. explicit
  1786. normal_distribution(result_type __mean,
  1787. result_type __stddev = result_type(1))
  1788. : _M_param(__mean, __stddev)
  1789. { }
  1790. explicit
  1791. normal_distribution(const param_type& __p)
  1792. : _M_param(__p)
  1793. { }
  1794. /**
  1795. * @brief Resets the distribution state.
  1796. */
  1797. void
  1798. reset()
  1799. { _M_saved_available = false; }
  1800. /**
  1801. * @brief Returns the mean of the distribution.
  1802. */
  1803. _RealType
  1804. mean() const
  1805. { return _M_param.mean(); }
  1806. /**
  1807. * @brief Returns the standard deviation of the distribution.
  1808. */
  1809. _RealType
  1810. stddev() const
  1811. { return _M_param.stddev(); }
  1812. /**
  1813. * @brief Returns the parameter set of the distribution.
  1814. */
  1815. param_type
  1816. param() const
  1817. { return _M_param; }
  1818. /**
  1819. * @brief Sets the parameter set of the distribution.
  1820. * @param __param The new parameter set of the distribution.
  1821. */
  1822. void
  1823. param(const param_type& __param)
  1824. { _M_param = __param; }
  1825. /**
  1826. * @brief Returns the greatest lower bound value of the distribution.
  1827. */
  1828. result_type
  1829. min() const
  1830. { return std::numeric_limits<result_type>::lowest(); }
  1831. /**
  1832. * @brief Returns the least upper bound value of the distribution.
  1833. */
  1834. result_type
  1835. max() const
  1836. { return std::numeric_limits<result_type>::max(); }
  1837. /**
  1838. * @brief Generating functions.
  1839. */
  1840. template<typename _UniformRandomNumberGenerator>
  1841. result_type
  1842. operator()(_UniformRandomNumberGenerator& __urng)
  1843. { return this->operator()(__urng, _M_param); }
  1844. template<typename _UniformRandomNumberGenerator>
  1845. result_type
  1846. operator()(_UniformRandomNumberGenerator& __urng,
  1847. const param_type& __p);
  1848. template<typename _ForwardIterator,
  1849. typename _UniformRandomNumberGenerator>
  1850. void
  1851. __generate(_ForwardIterator __f, _ForwardIterator __t,
  1852. _UniformRandomNumberGenerator& __urng)
  1853. { this->__generate(__f, __t, __urng, _M_param); }
  1854. template<typename _ForwardIterator,
  1855. typename _UniformRandomNumberGenerator>
  1856. void
  1857. __generate(_ForwardIterator __f, _ForwardIterator __t,
  1858. _UniformRandomNumberGenerator& __urng,
  1859. const param_type& __p)
  1860. { this->__generate_impl(__f, __t, __urng, __p); }
  1861. template<typename _UniformRandomNumberGenerator>
  1862. void
  1863. __generate(result_type* __f, result_type* __t,
  1864. _UniformRandomNumberGenerator& __urng,
  1865. const param_type& __p)
  1866. { this->__generate_impl(__f, __t, __urng, __p); }
  1867. /**
  1868. * @brief Return true if two normal distributions have
  1869. * the same parameters and the sequences that would
  1870. * be generated are equal.
  1871. */
  1872. template<typename _RealType1>
  1873. friend bool
  1874. operator==(const std::normal_distribution<_RealType1>& __d1,
  1875. const std::normal_distribution<_RealType1>& __d2);
  1876. /**
  1877. * @brief Inserts a %normal_distribution random number distribution
  1878. * @p __x into the output stream @p __os.
  1879. *
  1880. * @param __os An output stream.
  1881. * @param __x A %normal_distribution random number distribution.
  1882. *
  1883. * @returns The output stream with the state of @p __x inserted or in
  1884. * an error state.
  1885. */
  1886. template<typename _RealType1, typename _CharT, typename _Traits>
  1887. friend std::basic_ostream<_CharT, _Traits>&
  1888. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  1889. const std::normal_distribution<_RealType1>& __x);
  1890. /**
  1891. * @brief Extracts a %normal_distribution random number distribution
  1892. * @p __x from the input stream @p __is.
  1893. *
  1894. * @param __is An input stream.
  1895. * @param __x A %normal_distribution random number generator engine.
  1896. *
  1897. * @returns The input stream with @p __x extracted or in an error
  1898. * state.
  1899. */
  1900. template<typename _RealType1, typename _CharT, typename _Traits>
  1901. friend std::basic_istream<_CharT, _Traits>&
  1902. operator>>(std::basic_istream<_CharT, _Traits>& __is,
  1903. std::normal_distribution<_RealType1>& __x);
  1904. private:
  1905. template<typename _ForwardIterator,
  1906. typename _UniformRandomNumberGenerator>
  1907. void
  1908. __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
  1909. _UniformRandomNumberGenerator& __urng,
  1910. const param_type& __p);
  1911. param_type _M_param;
  1912. result_type _M_saved = 0;
  1913. bool _M_saved_available = false;
  1914. };
  1915. /**
  1916. * @brief Return true if two normal distributions are different.
  1917. */
  1918. template<typename _RealType>
  1919. inline bool
  1920. operator!=(const std::normal_distribution<_RealType>& __d1,
  1921. const std::normal_distribution<_RealType>& __d2)
  1922. { return !(__d1 == __d2); }
  1923. /**
  1924. * @brief A lognormal_distribution random number distribution.
  1925. *
  1926. * The formula for the normal probability mass function is
  1927. * @f[
  1928. * p(x|m,s) = \frac{1}{sx\sqrt{2\pi}}
  1929. * \exp{-\frac{(\ln{x} - m)^2}{2s^2}}
  1930. * @f]
  1931. */
  1932. template<typename _RealType = double>
  1933. class lognormal_distribution
  1934. {
  1935. static_assert(std::is_floating_point<_RealType>::value,
  1936. "result_type must be a floating point type");
  1937. public:
  1938. /** The type of the range of the distribution. */
  1939. typedef _RealType result_type;
  1940. /** Parameter type. */
  1941. struct param_type
  1942. {
  1943. typedef lognormal_distribution<_RealType> distribution_type;
  1944. param_type() : param_type(0.0) { }
  1945. explicit
  1946. param_type(_RealType __m, _RealType __s = _RealType(1))
  1947. : _M_m(__m), _M_s(__s)
  1948. { }
  1949. _RealType
  1950. m() const
  1951. { return _M_m; }
  1952. _RealType
  1953. s() const
  1954. { return _M_s; }
  1955. friend bool
  1956. operator==(const param_type& __p1, const param_type& __p2)
  1957. { return __p1._M_m == __p2._M_m && __p1._M_s == __p2._M_s; }
  1958. friend bool
  1959. operator!=(const param_type& __p1, const param_type& __p2)
  1960. { return !(__p1 == __p2); }
  1961. private:
  1962. _RealType _M_m;
  1963. _RealType _M_s;
  1964. };
  1965. lognormal_distribution() : lognormal_distribution(0.0) { }
  1966. explicit
  1967. lognormal_distribution(_RealType __m, _RealType __s = _RealType(1))
  1968. : _M_param(__m, __s), _M_nd()
  1969. { }
  1970. explicit
  1971. lognormal_distribution(const param_type& __p)
  1972. : _M_param(__p), _M_nd()
  1973. { }
  1974. /**
  1975. * Resets the distribution state.
  1976. */
  1977. void
  1978. reset()
  1979. { _M_nd.reset(); }
  1980. /**
  1981. *
  1982. */
  1983. _RealType
  1984. m() const
  1985. { return _M_param.m(); }
  1986. _RealType
  1987. s() const
  1988. { return _M_param.s(); }
  1989. /**
  1990. * @brief Returns the parameter set of the distribution.
  1991. */
  1992. param_type
  1993. param() const
  1994. { return _M_param; }
  1995. /**
  1996. * @brief Sets the parameter set of the distribution.
  1997. * @param __param The new parameter set of the distribution.
  1998. */
  1999. void
  2000. param(const param_type& __param)
  2001. { _M_param = __param; }
  2002. /**
  2003. * @brief Returns the greatest lower bound value of the distribution.
  2004. */
  2005. result_type
  2006. min() const
  2007. { return result_type(0); }
  2008. /**
  2009. * @brief Returns the least upper bound value of the distribution.
  2010. */
  2011. result_type
  2012. max() const
  2013. { return std::numeric_limits<result_type>::max(); }
  2014. /**
  2015. * @brief Generating functions.
  2016. */
  2017. template<typename _UniformRandomNumberGenerator>
  2018. result_type
  2019. operator()(_UniformRandomNumberGenerator& __urng)
  2020. { return this->operator()(__urng, _M_param); }
  2021. template<typename _UniformRandomNumberGenerator>
  2022. result_type
  2023. operator()(_UniformRandomNumberGenerator& __urng,
  2024. const param_type& __p)
  2025. { return std::exp(__p.s() * _M_nd(__urng) + __p.m()); }
  2026. template<typename _ForwardIterator,
  2027. typename _UniformRandomNumberGenerator>
  2028. void
  2029. __generate(_ForwardIterator __f, _ForwardIterator __t,
  2030. _UniformRandomNumberGenerator& __urng)
  2031. { this->__generate(__f, __t, __urng, _M_param); }
  2032. template<typename _ForwardIterator,
  2033. typename _UniformRandomNumberGenerator>
  2034. void
  2035. __generate(_ForwardIterator __f, _ForwardIterator __t,
  2036. _UniformRandomNumberGenerator& __urng,
  2037. const param_type& __p)
  2038. { this->__generate_impl(__f, __t, __urng, __p); }
  2039. template<typename _UniformRandomNumberGenerator>
  2040. void
  2041. __generate(result_type* __f, result_type* __t,
  2042. _UniformRandomNumberGenerator& __urng,
  2043. const param_type& __p)
  2044. { this->__generate_impl(__f, __t, __urng, __p); }
  2045. /**
  2046. * @brief Return true if two lognormal distributions have
  2047. * the same parameters and the sequences that would
  2048. * be generated are equal.
  2049. */
  2050. friend bool
  2051. operator==(const lognormal_distribution& __d1,
  2052. const lognormal_distribution& __d2)
  2053. { return (__d1._M_param == __d2._M_param
  2054. && __d1._M_nd == __d2._M_nd); }
  2055. /**
  2056. * @brief Inserts a %lognormal_distribution random number distribution
  2057. * @p __x into the output stream @p __os.
  2058. *
  2059. * @param __os An output stream.
  2060. * @param __x A %lognormal_distribution random number distribution.
  2061. *
  2062. * @returns The output stream with the state of @p __x inserted or in
  2063. * an error state.
  2064. */
  2065. template<typename _RealType1, typename _CharT, typename _Traits>
  2066. friend std::basic_ostream<_CharT, _Traits>&
  2067. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  2068. const std::lognormal_distribution<_RealType1>& __x);
  2069. /**
  2070. * @brief Extracts a %lognormal_distribution random number distribution
  2071. * @p __x from the input stream @p __is.
  2072. *
  2073. * @param __is An input stream.
  2074. * @param __x A %lognormal_distribution random number
  2075. * generator engine.
  2076. *
  2077. * @returns The input stream with @p __x extracted or in an error state.
  2078. */
  2079. template<typename _RealType1, typename _CharT, typename _Traits>
  2080. friend std::basic_istream<_CharT, _Traits>&
  2081. operator>>(std::basic_istream<_CharT, _Traits>& __is,
  2082. std::lognormal_distribution<_RealType1>& __x);
  2083. private:
  2084. template<typename _ForwardIterator,
  2085. typename _UniformRandomNumberGenerator>
  2086. void
  2087. __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
  2088. _UniformRandomNumberGenerator& __urng,
  2089. const param_type& __p);
  2090. param_type _M_param;
  2091. std::normal_distribution<result_type> _M_nd;
  2092. };
  2093. /**
  2094. * @brief Return true if two lognormal distributions are different.
  2095. */
  2096. template<typename _RealType>
  2097. inline bool
  2098. operator!=(const std::lognormal_distribution<_RealType>& __d1,
  2099. const std::lognormal_distribution<_RealType>& __d2)
  2100. { return !(__d1 == __d2); }
  2101. /**
  2102. * @brief A gamma continuous distribution for random numbers.
  2103. *
  2104. * The formula for the gamma probability density function is:
  2105. * @f[
  2106. * p(x|\alpha,\beta) = \frac{1}{\beta\Gamma(\alpha)}
  2107. * (x/\beta)^{\alpha - 1} e^{-x/\beta}
  2108. * @f]
  2109. */
  2110. template<typename _RealType = double>
  2111. class gamma_distribution
  2112. {
  2113. static_assert(std::is_floating_point<_RealType>::value,
  2114. "result_type must be a floating point type");
  2115. public:
  2116. /** The type of the range of the distribution. */
  2117. typedef _RealType result_type;
  2118. /** Parameter type. */
  2119. struct param_type
  2120. {
  2121. typedef gamma_distribution<_RealType> distribution_type;
  2122. friend class gamma_distribution<_RealType>;
  2123. param_type() : param_type(1.0) { }
  2124. explicit
  2125. param_type(_RealType __alpha_val, _RealType __beta_val = _RealType(1))
  2126. : _M_alpha(__alpha_val), _M_beta(__beta_val)
  2127. {
  2128. __glibcxx_assert(_M_alpha > _RealType(0));
  2129. _M_initialize();
  2130. }
  2131. _RealType
  2132. alpha() const
  2133. { return _M_alpha; }
  2134. _RealType
  2135. beta() const
  2136. { return _M_beta; }
  2137. friend bool
  2138. operator==(const param_type& __p1, const param_type& __p2)
  2139. { return (__p1._M_alpha == __p2._M_alpha
  2140. && __p1._M_beta == __p2._M_beta); }
  2141. friend bool
  2142. operator!=(const param_type& __p1, const param_type& __p2)
  2143. { return !(__p1 == __p2); }
  2144. private:
  2145. void
  2146. _M_initialize();
  2147. _RealType _M_alpha;
  2148. _RealType _M_beta;
  2149. _RealType _M_malpha, _M_a2;
  2150. };
  2151. public:
  2152. /**
  2153. * @brief Constructs a gamma distribution with parameters 1 and 1.
  2154. */
  2155. gamma_distribution() : gamma_distribution(1.0) { }
  2156. /**
  2157. * @brief Constructs a gamma distribution with parameters
  2158. * @f$\alpha@f$ and @f$\beta@f$.
  2159. */
  2160. explicit
  2161. gamma_distribution(_RealType __alpha_val,
  2162. _RealType __beta_val = _RealType(1))
  2163. : _M_param(__alpha_val, __beta_val), _M_nd()
  2164. { }
  2165. explicit
  2166. gamma_distribution(const param_type& __p)
  2167. : _M_param(__p), _M_nd()
  2168. { }
  2169. /**
  2170. * @brief Resets the distribution state.
  2171. */
  2172. void
  2173. reset()
  2174. { _M_nd.reset(); }
  2175. /**
  2176. * @brief Returns the @f$\alpha@f$ of the distribution.
  2177. */
  2178. _RealType
  2179. alpha() const
  2180. { return _M_param.alpha(); }
  2181. /**
  2182. * @brief Returns the @f$\beta@f$ of the distribution.
  2183. */
  2184. _RealType
  2185. beta() const
  2186. { return _M_param.beta(); }
  2187. /**
  2188. * @brief Returns the parameter set of the distribution.
  2189. */
  2190. param_type
  2191. param() const
  2192. { return _M_param; }
  2193. /**
  2194. * @brief Sets the parameter set of the distribution.
  2195. * @param __param The new parameter set of the distribution.
  2196. */
  2197. void
  2198. param(const param_type& __param)
  2199. { _M_param = __param; }
  2200. /**
  2201. * @brief Returns the greatest lower bound value of the distribution.
  2202. */
  2203. result_type
  2204. min() const
  2205. { return result_type(0); }
  2206. /**
  2207. * @brief Returns the least upper bound value of the distribution.
  2208. */
  2209. result_type
  2210. max() const
  2211. { return std::numeric_limits<result_type>::max(); }
  2212. /**
  2213. * @brief Generating functions.
  2214. */
  2215. template<typename _UniformRandomNumberGenerator>
  2216. result_type
  2217. operator()(_UniformRandomNumberGenerator& __urng)
  2218. { return this->operator()(__urng, _M_param); }
  2219. template<typename _UniformRandomNumberGenerator>
  2220. result_type
  2221. operator()(_UniformRandomNumberGenerator& __urng,
  2222. const param_type& __p);
  2223. template<typename _ForwardIterator,
  2224. typename _UniformRandomNumberGenerator>
  2225. void
  2226. __generate(_ForwardIterator __f, _ForwardIterator __t,
  2227. _UniformRandomNumberGenerator& __urng)
  2228. { this->__generate(__f, __t, __urng, _M_param); }
  2229. template<typename _ForwardIterator,
  2230. typename _UniformRandomNumberGenerator>
  2231. void
  2232. __generate(_ForwardIterator __f, _ForwardIterator __t,
  2233. _UniformRandomNumberGenerator& __urng,
  2234. const param_type& __p)
  2235. { this->__generate_impl(__f, __t, __urng, __p); }
  2236. template<typename _UniformRandomNumberGenerator>
  2237. void
  2238. __generate(result_type* __f, result_type* __t,
  2239. _UniformRandomNumberGenerator& __urng,
  2240. const param_type& __p)
  2241. { this->__generate_impl(__f, __t, __urng, __p); }
  2242. /**
  2243. * @brief Return true if two gamma distributions have the same
  2244. * parameters and the sequences that would be generated
  2245. * are equal.
  2246. */
  2247. friend bool
  2248. operator==(const gamma_distribution& __d1,
  2249. const gamma_distribution& __d2)
  2250. { return (__d1._M_param == __d2._M_param
  2251. && __d1._M_nd == __d2._M_nd); }
  2252. /**
  2253. * @brief Inserts a %gamma_distribution random number distribution
  2254. * @p __x into the output stream @p __os.
  2255. *
  2256. * @param __os An output stream.
  2257. * @param __x A %gamma_distribution random number distribution.
  2258. *
  2259. * @returns The output stream with the state of @p __x inserted or in
  2260. * an error state.
  2261. */
  2262. template<typename _RealType1, typename _CharT, typename _Traits>
  2263. friend std::basic_ostream<_CharT, _Traits>&
  2264. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  2265. const std::gamma_distribution<_RealType1>& __x);
  2266. /**
  2267. * @brief Extracts a %gamma_distribution random number distribution
  2268. * @p __x from the input stream @p __is.
  2269. *
  2270. * @param __is An input stream.
  2271. * @param __x A %gamma_distribution random number generator engine.
  2272. *
  2273. * @returns The input stream with @p __x extracted or in an error state.
  2274. */
  2275. template<typename _RealType1, typename _CharT, typename _Traits>
  2276. friend std::basic_istream<_CharT, _Traits>&
  2277. operator>>(std::basic_istream<_CharT, _Traits>& __is,
  2278. std::gamma_distribution<_RealType1>& __x);
  2279. private:
  2280. template<typename _ForwardIterator,
  2281. typename _UniformRandomNumberGenerator>
  2282. void
  2283. __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
  2284. _UniformRandomNumberGenerator& __urng,
  2285. const param_type& __p);
  2286. param_type _M_param;
  2287. std::normal_distribution<result_type> _M_nd;
  2288. };
  2289. /**
  2290. * @brief Return true if two gamma distributions are different.
  2291. */
  2292. template<typename _RealType>
  2293. inline bool
  2294. operator!=(const std::gamma_distribution<_RealType>& __d1,
  2295. const std::gamma_distribution<_RealType>& __d2)
  2296. { return !(__d1 == __d2); }
  2297. /**
  2298. * @brief A chi_squared_distribution random number distribution.
  2299. *
  2300. * The formula for the normal probability mass function is
  2301. * @f$p(x|n) = \frac{x^{(n/2) - 1}e^{-x/2}}{\Gamma(n/2) 2^{n/2}}@f$
  2302. */
  2303. template<typename _RealType = double>
  2304. class chi_squared_distribution
  2305. {
  2306. static_assert(std::is_floating_point<_RealType>::value,
  2307. "result_type must be a floating point type");
  2308. public:
  2309. /** The type of the range of the distribution. */
  2310. typedef _RealType result_type;
  2311. /** Parameter type. */
  2312. struct param_type
  2313. {
  2314. typedef chi_squared_distribution<_RealType> distribution_type;
  2315. param_type() : param_type(1) { }
  2316. explicit
  2317. param_type(_RealType __n)
  2318. : _M_n(__n)
  2319. { }
  2320. _RealType
  2321. n() const
  2322. { return _M_n; }
  2323. friend bool
  2324. operator==(const param_type& __p1, const param_type& __p2)
  2325. { return __p1._M_n == __p2._M_n; }
  2326. friend bool
  2327. operator!=(const param_type& __p1, const param_type& __p2)
  2328. { return !(__p1 == __p2); }
  2329. private:
  2330. _RealType _M_n;
  2331. };
  2332. chi_squared_distribution() : chi_squared_distribution(1) { }
  2333. explicit
  2334. chi_squared_distribution(_RealType __n)
  2335. : _M_param(__n), _M_gd(__n / 2)
  2336. { }
  2337. explicit
  2338. chi_squared_distribution(const param_type& __p)
  2339. : _M_param(__p), _M_gd(__p.n() / 2)
  2340. { }
  2341. /**
  2342. * @brief Resets the distribution state.
  2343. */
  2344. void
  2345. reset()
  2346. { _M_gd.reset(); }
  2347. /**
  2348. *
  2349. */
  2350. _RealType
  2351. n() const
  2352. { return _M_param.n(); }
  2353. /**
  2354. * @brief Returns the parameter set of the distribution.
  2355. */
  2356. param_type
  2357. param() const
  2358. { return _M_param; }
  2359. /**
  2360. * @brief Sets the parameter set of the distribution.
  2361. * @param __param The new parameter set of the distribution.
  2362. */
  2363. void
  2364. param(const param_type& __param)
  2365. {
  2366. _M_param = __param;
  2367. typedef typename std::gamma_distribution<result_type>::param_type
  2368. param_type;
  2369. _M_gd.param(param_type{__param.n() / 2});
  2370. }
  2371. /**
  2372. * @brief Returns the greatest lower bound value of the distribution.
  2373. */
  2374. result_type
  2375. min() const
  2376. { return result_type(0); }
  2377. /**
  2378. * @brief Returns the least upper bound value of the distribution.
  2379. */
  2380. result_type
  2381. max() const
  2382. { return std::numeric_limits<result_type>::max(); }
  2383. /**
  2384. * @brief Generating functions.
  2385. */
  2386. template<typename _UniformRandomNumberGenerator>
  2387. result_type
  2388. operator()(_UniformRandomNumberGenerator& __urng)
  2389. { return 2 * _M_gd(__urng); }
  2390. template<typename _UniformRandomNumberGenerator>
  2391. result_type
  2392. operator()(_UniformRandomNumberGenerator& __urng,
  2393. const param_type& __p)
  2394. {
  2395. typedef typename std::gamma_distribution<result_type>::param_type
  2396. param_type;
  2397. return 2 * _M_gd(__urng, param_type(__p.n() / 2));
  2398. }
  2399. template<typename _ForwardIterator,
  2400. typename _UniformRandomNumberGenerator>
  2401. void
  2402. __generate(_ForwardIterator __f, _ForwardIterator __t,
  2403. _UniformRandomNumberGenerator& __urng)
  2404. { this->__generate_impl(__f, __t, __urng); }
  2405. template<typename _ForwardIterator,
  2406. typename _UniformRandomNumberGenerator>
  2407. void
  2408. __generate(_ForwardIterator __f, _ForwardIterator __t,
  2409. _UniformRandomNumberGenerator& __urng,
  2410. const param_type& __p)
  2411. { typename std::gamma_distribution<result_type>::param_type
  2412. __p2(__p.n() / 2);
  2413. this->__generate_impl(__f, __t, __urng, __p2); }
  2414. template<typename _UniformRandomNumberGenerator>
  2415. void
  2416. __generate(result_type* __f, result_type* __t,
  2417. _UniformRandomNumberGenerator& __urng)
  2418. { this->__generate_impl(__f, __t, __urng); }
  2419. template<typename _UniformRandomNumberGenerator>
  2420. void
  2421. __generate(result_type* __f, result_type* __t,
  2422. _UniformRandomNumberGenerator& __urng,
  2423. const param_type& __p)
  2424. { typename std::gamma_distribution<result_type>::param_type
  2425. __p2(__p.n() / 2);
  2426. this->__generate_impl(__f, __t, __urng, __p2); }
  2427. /**
  2428. * @brief Return true if two Chi-squared distributions have
  2429. * the same parameters and the sequences that would be
  2430. * generated are equal.
  2431. */
  2432. friend bool
  2433. operator==(const chi_squared_distribution& __d1,
  2434. const chi_squared_distribution& __d2)
  2435. { return __d1._M_param == __d2._M_param && __d1._M_gd == __d2._M_gd; }
  2436. /**
  2437. * @brief Inserts a %chi_squared_distribution random number distribution
  2438. * @p __x into the output stream @p __os.
  2439. *
  2440. * @param __os An output stream.
  2441. * @param __x A %chi_squared_distribution random number distribution.
  2442. *
  2443. * @returns The output stream with the state of @p __x inserted or in
  2444. * an error state.
  2445. */
  2446. template<typename _RealType1, typename _CharT, typename _Traits>
  2447. friend std::basic_ostream<_CharT, _Traits>&
  2448. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  2449. const std::chi_squared_distribution<_RealType1>& __x);
  2450. /**
  2451. * @brief Extracts a %chi_squared_distribution random number distribution
  2452. * @p __x from the input stream @p __is.
  2453. *
  2454. * @param __is An input stream.
  2455. * @param __x A %chi_squared_distribution random number
  2456. * generator engine.
  2457. *
  2458. * @returns The input stream with @p __x extracted or in an error state.
  2459. */
  2460. template<typename _RealType1, typename _CharT, typename _Traits>
  2461. friend std::basic_istream<_CharT, _Traits>&
  2462. operator>>(std::basic_istream<_CharT, _Traits>& __is,
  2463. std::chi_squared_distribution<_RealType1>& __x);
  2464. private:
  2465. template<typename _ForwardIterator,
  2466. typename _UniformRandomNumberGenerator>
  2467. void
  2468. __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
  2469. _UniformRandomNumberGenerator& __urng);
  2470. template<typename _ForwardIterator,
  2471. typename _UniformRandomNumberGenerator>
  2472. void
  2473. __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
  2474. _UniformRandomNumberGenerator& __urng,
  2475. const typename
  2476. std::gamma_distribution<result_type>::param_type& __p);
  2477. param_type _M_param;
  2478. std::gamma_distribution<result_type> _M_gd;
  2479. };
  2480. /**
  2481. * @brief Return true if two Chi-squared distributions are different.
  2482. */
  2483. template<typename _RealType>
  2484. inline bool
  2485. operator!=(const std::chi_squared_distribution<_RealType>& __d1,
  2486. const std::chi_squared_distribution<_RealType>& __d2)
  2487. { return !(__d1 == __d2); }
  2488. /**
  2489. * @brief A cauchy_distribution random number distribution.
  2490. *
  2491. * The formula for the normal probability mass function is
  2492. * @f$p(x|a,b) = (\pi b (1 + (\frac{x-a}{b})^2))^{-1}@f$
  2493. */
  2494. template<typename _RealType = double>
  2495. class cauchy_distribution
  2496. {
  2497. static_assert(std::is_floating_point<_RealType>::value,
  2498. "result_type must be a floating point type");
  2499. public:
  2500. /** The type of the range of the distribution. */
  2501. typedef _RealType result_type;
  2502. /** Parameter type. */
  2503. struct param_type
  2504. {
  2505. typedef cauchy_distribution<_RealType> distribution_type;
  2506. param_type() : param_type(0) { }
  2507. explicit
  2508. param_type(_RealType __a, _RealType __b = _RealType(1))
  2509. : _M_a(__a), _M_b(__b)
  2510. { }
  2511. _RealType
  2512. a() const
  2513. { return _M_a; }
  2514. _RealType
  2515. b() const
  2516. { return _M_b; }
  2517. friend bool
  2518. operator==(const param_type& __p1, const param_type& __p2)
  2519. { return __p1._M_a == __p2._M_a && __p1._M_b == __p2._M_b; }
  2520. friend bool
  2521. operator!=(const param_type& __p1, const param_type& __p2)
  2522. { return !(__p1 == __p2); }
  2523. private:
  2524. _RealType _M_a;
  2525. _RealType _M_b;
  2526. };
  2527. cauchy_distribution() : cauchy_distribution(0.0) { }
  2528. explicit
  2529. cauchy_distribution(_RealType __a, _RealType __b = 1.0)
  2530. : _M_param(__a, __b)
  2531. { }
  2532. explicit
  2533. cauchy_distribution(const param_type& __p)
  2534. : _M_param(__p)
  2535. { }
  2536. /**
  2537. * @brief Resets the distribution state.
  2538. */
  2539. void
  2540. reset()
  2541. { }
  2542. /**
  2543. *
  2544. */
  2545. _RealType
  2546. a() const
  2547. { return _M_param.a(); }
  2548. _RealType
  2549. b() const
  2550. { return _M_param.b(); }
  2551. /**
  2552. * @brief Returns the parameter set of the distribution.
  2553. */
  2554. param_type
  2555. param() const
  2556. { return _M_param; }
  2557. /**
  2558. * @brief Sets the parameter set of the distribution.
  2559. * @param __param The new parameter set of the distribution.
  2560. */
  2561. void
  2562. param(const param_type& __param)
  2563. { _M_param = __param; }
  2564. /**
  2565. * @brief Returns the greatest lower bound value of the distribution.
  2566. */
  2567. result_type
  2568. min() const
  2569. { return std::numeric_limits<result_type>::lowest(); }
  2570. /**
  2571. * @brief Returns the least upper bound value of the distribution.
  2572. */
  2573. result_type
  2574. max() const
  2575. { return std::numeric_limits<result_type>::max(); }
  2576. /**
  2577. * @brief Generating functions.
  2578. */
  2579. template<typename _UniformRandomNumberGenerator>
  2580. result_type
  2581. operator()(_UniformRandomNumberGenerator& __urng)
  2582. { return this->operator()(__urng, _M_param); }
  2583. template<typename _UniformRandomNumberGenerator>
  2584. result_type
  2585. operator()(_UniformRandomNumberGenerator& __urng,
  2586. const param_type& __p);
  2587. template<typename _ForwardIterator,
  2588. typename _UniformRandomNumberGenerator>
  2589. void
  2590. __generate(_ForwardIterator __f, _ForwardIterator __t,
  2591. _UniformRandomNumberGenerator& __urng)
  2592. { this->__generate(__f, __t, __urng, _M_param); }
  2593. template<typename _ForwardIterator,
  2594. typename _UniformRandomNumberGenerator>
  2595. void
  2596. __generate(_ForwardIterator __f, _ForwardIterator __t,
  2597. _UniformRandomNumberGenerator& __urng,
  2598. const param_type& __p)
  2599. { this->__generate_impl(__f, __t, __urng, __p); }
  2600. template<typename _UniformRandomNumberGenerator>
  2601. void
  2602. __generate(result_type* __f, result_type* __t,
  2603. _UniformRandomNumberGenerator& __urng,
  2604. const param_type& __p)
  2605. { this->__generate_impl(__f, __t, __urng, __p); }
  2606. /**
  2607. * @brief Return true if two Cauchy distributions have
  2608. * the same parameters.
  2609. */
  2610. friend bool
  2611. operator==(const cauchy_distribution& __d1,
  2612. const cauchy_distribution& __d2)
  2613. { return __d1._M_param == __d2._M_param; }
  2614. private:
  2615. template<typename _ForwardIterator,
  2616. typename _UniformRandomNumberGenerator>
  2617. void
  2618. __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
  2619. _UniformRandomNumberGenerator& __urng,
  2620. const param_type& __p);
  2621. param_type _M_param;
  2622. };
  2623. /**
  2624. * @brief Return true if two Cauchy distributions have
  2625. * different parameters.
  2626. */
  2627. template<typename _RealType>
  2628. inline bool
  2629. operator!=(const std::cauchy_distribution<_RealType>& __d1,
  2630. const std::cauchy_distribution<_RealType>& __d2)
  2631. { return !(__d1 == __d2); }
  2632. /**
  2633. * @brief Inserts a %cauchy_distribution random number distribution
  2634. * @p __x into the output stream @p __os.
  2635. *
  2636. * @param __os An output stream.
  2637. * @param __x A %cauchy_distribution random number distribution.
  2638. *
  2639. * @returns The output stream with the state of @p __x inserted or in
  2640. * an error state.
  2641. */
  2642. template<typename _RealType, typename _CharT, typename _Traits>
  2643. std::basic_ostream<_CharT, _Traits>&
  2644. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  2645. const std::cauchy_distribution<_RealType>& __x);
  2646. /**
  2647. * @brief Extracts a %cauchy_distribution random number distribution
  2648. * @p __x from the input stream @p __is.
  2649. *
  2650. * @param __is An input stream.
  2651. * @param __x A %cauchy_distribution random number
  2652. * generator engine.
  2653. *
  2654. * @returns The input stream with @p __x extracted or in an error state.
  2655. */
  2656. template<typename _RealType, typename _CharT, typename _Traits>
  2657. std::basic_istream<_CharT, _Traits>&
  2658. operator>>(std::basic_istream<_CharT, _Traits>& __is,
  2659. std::cauchy_distribution<_RealType>& __x);
  2660. /**
  2661. * @brief A fisher_f_distribution random number distribution.
  2662. *
  2663. * The formula for the normal probability mass function is
  2664. * @f[
  2665. * p(x|m,n) = \frac{\Gamma((m+n)/2)}{\Gamma(m/2)\Gamma(n/2)}
  2666. * (\frac{m}{n})^{m/2} x^{(m/2)-1}
  2667. * (1 + \frac{mx}{n})^{-(m+n)/2}
  2668. * @f]
  2669. */
  2670. template<typename _RealType = double>
  2671. class fisher_f_distribution
  2672. {
  2673. static_assert(std::is_floating_point<_RealType>::value,
  2674. "result_type must be a floating point type");
  2675. public:
  2676. /** The type of the range of the distribution. */
  2677. typedef _RealType result_type;
  2678. /** Parameter type. */
  2679. struct param_type
  2680. {
  2681. typedef fisher_f_distribution<_RealType> distribution_type;
  2682. param_type() : param_type(1) { }
  2683. explicit
  2684. param_type(_RealType __m, _RealType __n = _RealType(1))
  2685. : _M_m(__m), _M_n(__n)
  2686. { }
  2687. _RealType
  2688. m() const
  2689. { return _M_m; }
  2690. _RealType
  2691. n() const
  2692. { return _M_n; }
  2693. friend bool
  2694. operator==(const param_type& __p1, const param_type& __p2)
  2695. { return __p1._M_m == __p2._M_m && __p1._M_n == __p2._M_n; }
  2696. friend bool
  2697. operator!=(const param_type& __p1, const param_type& __p2)
  2698. { return !(__p1 == __p2); }
  2699. private:
  2700. _RealType _M_m;
  2701. _RealType _M_n;
  2702. };
  2703. fisher_f_distribution() : fisher_f_distribution(1.0) { }
  2704. explicit
  2705. fisher_f_distribution(_RealType __m,
  2706. _RealType __n = _RealType(1))
  2707. : _M_param(__m, __n), _M_gd_x(__m / 2), _M_gd_y(__n / 2)
  2708. { }
  2709. explicit
  2710. fisher_f_distribution(const param_type& __p)
  2711. : _M_param(__p), _M_gd_x(__p.m() / 2), _M_gd_y(__p.n() / 2)
  2712. { }
  2713. /**
  2714. * @brief Resets the distribution state.
  2715. */
  2716. void
  2717. reset()
  2718. {
  2719. _M_gd_x.reset();
  2720. _M_gd_y.reset();
  2721. }
  2722. /**
  2723. *
  2724. */
  2725. _RealType
  2726. m() const
  2727. { return _M_param.m(); }
  2728. _RealType
  2729. n() const
  2730. { return _M_param.n(); }
  2731. /**
  2732. * @brief Returns the parameter set of the distribution.
  2733. */
  2734. param_type
  2735. param() const
  2736. { return _M_param; }
  2737. /**
  2738. * @brief Sets the parameter set of the distribution.
  2739. * @param __param The new parameter set of the distribution.
  2740. */
  2741. void
  2742. param(const param_type& __param)
  2743. { _M_param = __param; }
  2744. /**
  2745. * @brief Returns the greatest lower bound value of the distribution.
  2746. */
  2747. result_type
  2748. min() const
  2749. { return result_type(0); }
  2750. /**
  2751. * @brief Returns the least upper bound value of the distribution.
  2752. */
  2753. result_type
  2754. max() const
  2755. { return std::numeric_limits<result_type>::max(); }
  2756. /**
  2757. * @brief Generating functions.
  2758. */
  2759. template<typename _UniformRandomNumberGenerator>
  2760. result_type
  2761. operator()(_UniformRandomNumberGenerator& __urng)
  2762. { return (_M_gd_x(__urng) * n()) / (_M_gd_y(__urng) * m()); }
  2763. template<typename _UniformRandomNumberGenerator>
  2764. result_type
  2765. operator()(_UniformRandomNumberGenerator& __urng,
  2766. const param_type& __p)
  2767. {
  2768. typedef typename std::gamma_distribution<result_type>::param_type
  2769. param_type;
  2770. return ((_M_gd_x(__urng, param_type(__p.m() / 2)) * n())
  2771. / (_M_gd_y(__urng, param_type(__p.n() / 2)) * m()));
  2772. }
  2773. template<typename _ForwardIterator,
  2774. typename _UniformRandomNumberGenerator>
  2775. void
  2776. __generate(_ForwardIterator __f, _ForwardIterator __t,
  2777. _UniformRandomNumberGenerator& __urng)
  2778. { this->__generate_impl(__f, __t, __urng); }
  2779. template<typename _ForwardIterator,
  2780. typename _UniformRandomNumberGenerator>
  2781. void
  2782. __generate(_ForwardIterator __f, _ForwardIterator __t,
  2783. _UniformRandomNumberGenerator& __urng,
  2784. const param_type& __p)
  2785. { this->__generate_impl(__f, __t, __urng, __p); }
  2786. template<typename _UniformRandomNumberGenerator>
  2787. void
  2788. __generate(result_type* __f, result_type* __t,
  2789. _UniformRandomNumberGenerator& __urng)
  2790. { this->__generate_impl(__f, __t, __urng); }
  2791. template<typename _UniformRandomNumberGenerator>
  2792. void
  2793. __generate(result_type* __f, result_type* __t,
  2794. _UniformRandomNumberGenerator& __urng,
  2795. const param_type& __p)
  2796. { this->__generate_impl(__f, __t, __urng, __p); }
  2797. /**
  2798. * @brief Return true if two Fisher f distributions have
  2799. * the same parameters and the sequences that would
  2800. * be generated are equal.
  2801. */
  2802. friend bool
  2803. operator==(const fisher_f_distribution& __d1,
  2804. const fisher_f_distribution& __d2)
  2805. { return (__d1._M_param == __d2._M_param
  2806. && __d1._M_gd_x == __d2._M_gd_x
  2807. && __d1._M_gd_y == __d2._M_gd_y); }
  2808. /**
  2809. * @brief Inserts a %fisher_f_distribution random number distribution
  2810. * @p __x into the output stream @p __os.
  2811. *
  2812. * @param __os An output stream.
  2813. * @param __x A %fisher_f_distribution random number distribution.
  2814. *
  2815. * @returns The output stream with the state of @p __x inserted or in
  2816. * an error state.
  2817. */
  2818. template<typename _RealType1, typename _CharT, typename _Traits>
  2819. friend std::basic_ostream<_CharT, _Traits>&
  2820. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  2821. const std::fisher_f_distribution<_RealType1>& __x);
  2822. /**
  2823. * @brief Extracts a %fisher_f_distribution random number distribution
  2824. * @p __x from the input stream @p __is.
  2825. *
  2826. * @param __is An input stream.
  2827. * @param __x A %fisher_f_distribution random number
  2828. * generator engine.
  2829. *
  2830. * @returns The input stream with @p __x extracted or in an error state.
  2831. */
  2832. template<typename _RealType1, typename _CharT, typename _Traits>
  2833. friend std::basic_istream<_CharT, _Traits>&
  2834. operator>>(std::basic_istream<_CharT, _Traits>& __is,
  2835. std::fisher_f_distribution<_RealType1>& __x);
  2836. private:
  2837. template<typename _ForwardIterator,
  2838. typename _UniformRandomNumberGenerator>
  2839. void
  2840. __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
  2841. _UniformRandomNumberGenerator& __urng);
  2842. template<typename _ForwardIterator,
  2843. typename _UniformRandomNumberGenerator>
  2844. void
  2845. __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
  2846. _UniformRandomNumberGenerator& __urng,
  2847. const param_type& __p);
  2848. param_type _M_param;
  2849. std::gamma_distribution<result_type> _M_gd_x, _M_gd_y;
  2850. };
  2851. /**
  2852. * @brief Return true if two Fisher f distributions are different.
  2853. */
  2854. template<typename _RealType>
  2855. inline bool
  2856. operator!=(const std::fisher_f_distribution<_RealType>& __d1,
  2857. const std::fisher_f_distribution<_RealType>& __d2)
  2858. { return !(__d1 == __d2); }
  2859. /**
  2860. * @brief A student_t_distribution random number distribution.
  2861. *
  2862. * The formula for the normal probability mass function is:
  2863. * @f[
  2864. * p(x|n) = \frac{1}{\sqrt(n\pi)} \frac{\Gamma((n+1)/2)}{\Gamma(n/2)}
  2865. * (1 + \frac{x^2}{n}) ^{-(n+1)/2}
  2866. * @f]
  2867. */
  2868. template<typename _RealType = double>
  2869. class student_t_distribution
  2870. {
  2871. static_assert(std::is_floating_point<_RealType>::value,
  2872. "result_type must be a floating point type");
  2873. public:
  2874. /** The type of the range of the distribution. */
  2875. typedef _RealType result_type;
  2876. /** Parameter type. */
  2877. struct param_type
  2878. {
  2879. typedef student_t_distribution<_RealType> distribution_type;
  2880. param_type() : param_type(1) { }
  2881. explicit
  2882. param_type(_RealType __n)
  2883. : _M_n(__n)
  2884. { }
  2885. _RealType
  2886. n() const
  2887. { return _M_n; }
  2888. friend bool
  2889. operator==(const param_type& __p1, const param_type& __p2)
  2890. { return __p1._M_n == __p2._M_n; }
  2891. friend bool
  2892. operator!=(const param_type& __p1, const param_type& __p2)
  2893. { return !(__p1 == __p2); }
  2894. private:
  2895. _RealType _M_n;
  2896. };
  2897. student_t_distribution() : student_t_distribution(1.0) { }
  2898. explicit
  2899. student_t_distribution(_RealType __n)
  2900. : _M_param(__n), _M_nd(), _M_gd(__n / 2, 2)
  2901. { }
  2902. explicit
  2903. student_t_distribution(const param_type& __p)
  2904. : _M_param(__p), _M_nd(), _M_gd(__p.n() / 2, 2)
  2905. { }
  2906. /**
  2907. * @brief Resets the distribution state.
  2908. */
  2909. void
  2910. reset()
  2911. {
  2912. _M_nd.reset();
  2913. _M_gd.reset();
  2914. }
  2915. /**
  2916. *
  2917. */
  2918. _RealType
  2919. n() const
  2920. { return _M_param.n(); }
  2921. /**
  2922. * @brief Returns the parameter set of the distribution.
  2923. */
  2924. param_type
  2925. param() const
  2926. { return _M_param; }
  2927. /**
  2928. * @brief Sets the parameter set of the distribution.
  2929. * @param __param The new parameter set of the distribution.
  2930. */
  2931. void
  2932. param(const param_type& __param)
  2933. { _M_param = __param; }
  2934. /**
  2935. * @brief Returns the greatest lower bound value of the distribution.
  2936. */
  2937. result_type
  2938. min() const
  2939. { return std::numeric_limits<result_type>::lowest(); }
  2940. /**
  2941. * @brief Returns the least upper bound value of the distribution.
  2942. */
  2943. result_type
  2944. max() const
  2945. { return std::numeric_limits<result_type>::max(); }
  2946. /**
  2947. * @brief Generating functions.
  2948. */
  2949. template<typename _UniformRandomNumberGenerator>
  2950. result_type
  2951. operator()(_UniformRandomNumberGenerator& __urng)
  2952. { return _M_nd(__urng) * std::sqrt(n() / _M_gd(__urng)); }
  2953. template<typename _UniformRandomNumberGenerator>
  2954. result_type
  2955. operator()(_UniformRandomNumberGenerator& __urng,
  2956. const param_type& __p)
  2957. {
  2958. typedef typename std::gamma_distribution<result_type>::param_type
  2959. param_type;
  2960. const result_type __g = _M_gd(__urng, param_type(__p.n() / 2, 2));
  2961. return _M_nd(__urng) * std::sqrt(__p.n() / __g);
  2962. }
  2963. template<typename _ForwardIterator,
  2964. typename _UniformRandomNumberGenerator>
  2965. void
  2966. __generate(_ForwardIterator __f, _ForwardIterator __t,
  2967. _UniformRandomNumberGenerator& __urng)
  2968. { this->__generate_impl(__f, __t, __urng); }
  2969. template<typename _ForwardIterator,
  2970. typename _UniformRandomNumberGenerator>
  2971. void
  2972. __generate(_ForwardIterator __f, _ForwardIterator __t,
  2973. _UniformRandomNumberGenerator& __urng,
  2974. const param_type& __p)
  2975. { this->__generate_impl(__f, __t, __urng, __p); }
  2976. template<typename _UniformRandomNumberGenerator>
  2977. void
  2978. __generate(result_type* __f, result_type* __t,
  2979. _UniformRandomNumberGenerator& __urng)
  2980. { this->__generate_impl(__f, __t, __urng); }
  2981. template<typename _UniformRandomNumberGenerator>
  2982. void
  2983. __generate(result_type* __f, result_type* __t,
  2984. _UniformRandomNumberGenerator& __urng,
  2985. const param_type& __p)
  2986. { this->__generate_impl(__f, __t, __urng, __p); }
  2987. /**
  2988. * @brief Return true if two Student t distributions have
  2989. * the same parameters and the sequences that would
  2990. * be generated are equal.
  2991. */
  2992. friend bool
  2993. operator==(const student_t_distribution& __d1,
  2994. const student_t_distribution& __d2)
  2995. { return (__d1._M_param == __d2._M_param
  2996. && __d1._M_nd == __d2._M_nd && __d1._M_gd == __d2._M_gd); }
  2997. /**
  2998. * @brief Inserts a %student_t_distribution random number distribution
  2999. * @p __x into the output stream @p __os.
  3000. *
  3001. * @param __os An output stream.
  3002. * @param __x A %student_t_distribution random number distribution.
  3003. *
  3004. * @returns The output stream with the state of @p __x inserted or in
  3005. * an error state.
  3006. */
  3007. template<typename _RealType1, typename _CharT, typename _Traits>
  3008. friend std::basic_ostream<_CharT, _Traits>&
  3009. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  3010. const std::student_t_distribution<_RealType1>& __x);
  3011. /**
  3012. * @brief Extracts a %student_t_distribution random number distribution
  3013. * @p __x from the input stream @p __is.
  3014. *
  3015. * @param __is An input stream.
  3016. * @param __x A %student_t_distribution random number
  3017. * generator engine.
  3018. *
  3019. * @returns The input stream with @p __x extracted or in an error state.
  3020. */
  3021. template<typename _RealType1, typename _CharT, typename _Traits>
  3022. friend std::basic_istream<_CharT, _Traits>&
  3023. operator>>(std::basic_istream<_CharT, _Traits>& __is,
  3024. std::student_t_distribution<_RealType1>& __x);
  3025. private:
  3026. template<typename _ForwardIterator,
  3027. typename _UniformRandomNumberGenerator>
  3028. void
  3029. __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
  3030. _UniformRandomNumberGenerator& __urng);
  3031. template<typename _ForwardIterator,
  3032. typename _UniformRandomNumberGenerator>
  3033. void
  3034. __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
  3035. _UniformRandomNumberGenerator& __urng,
  3036. const param_type& __p);
  3037. param_type _M_param;
  3038. std::normal_distribution<result_type> _M_nd;
  3039. std::gamma_distribution<result_type> _M_gd;
  3040. };
  3041. /**
  3042. * @brief Return true if two Student t distributions are different.
  3043. */
  3044. template<typename _RealType>
  3045. inline bool
  3046. operator!=(const std::student_t_distribution<_RealType>& __d1,
  3047. const std::student_t_distribution<_RealType>& __d2)
  3048. { return !(__d1 == __d2); }
  3049. /// @} group random_distributions_normal
  3050. /**
  3051. * @addtogroup random_distributions_bernoulli Bernoulli Distributions
  3052. * @ingroup random_distributions
  3053. * @{
  3054. */
  3055. /**
  3056. * @brief A Bernoulli random number distribution.
  3057. *
  3058. * Generates a sequence of true and false values with likelihood @f$p@f$
  3059. * that true will come up and @f$(1 - p)@f$ that false will appear.
  3060. */
  3061. class bernoulli_distribution
  3062. {
  3063. public:
  3064. /** The type of the range of the distribution. */
  3065. typedef bool result_type;
  3066. /** Parameter type. */
  3067. struct param_type
  3068. {
  3069. typedef bernoulli_distribution distribution_type;
  3070. param_type() : param_type(0.5) { }
  3071. explicit
  3072. param_type(double __p)
  3073. : _M_p(__p)
  3074. {
  3075. __glibcxx_assert((_M_p >= 0.0) && (_M_p <= 1.0));
  3076. }
  3077. double
  3078. p() const
  3079. { return _M_p; }
  3080. friend bool
  3081. operator==(const param_type& __p1, const param_type& __p2)
  3082. { return __p1._M_p == __p2._M_p; }
  3083. friend bool
  3084. operator!=(const param_type& __p1, const param_type& __p2)
  3085. { return !(__p1 == __p2); }
  3086. private:
  3087. double _M_p;
  3088. };
  3089. public:
  3090. /**
  3091. * @brief Constructs a Bernoulli distribution with likelihood 0.5.
  3092. */
  3093. bernoulli_distribution() : bernoulli_distribution(0.5) { }
  3094. /**
  3095. * @brief Constructs a Bernoulli distribution with likelihood @p p.
  3096. *
  3097. * @param __p [IN] The likelihood of a true result being returned.
  3098. * Must be in the interval @f$[0, 1]@f$.
  3099. */
  3100. explicit
  3101. bernoulli_distribution(double __p)
  3102. : _M_param(__p)
  3103. { }
  3104. explicit
  3105. bernoulli_distribution(const param_type& __p)
  3106. : _M_param(__p)
  3107. { }
  3108. /**
  3109. * @brief Resets the distribution state.
  3110. *
  3111. * Does nothing for a Bernoulli distribution.
  3112. */
  3113. void
  3114. reset() { }
  3115. /**
  3116. * @brief Returns the @p p parameter of the distribution.
  3117. */
  3118. double
  3119. p() const
  3120. { return _M_param.p(); }
  3121. /**
  3122. * @brief Returns the parameter set of the distribution.
  3123. */
  3124. param_type
  3125. param() const
  3126. { return _M_param; }
  3127. /**
  3128. * @brief Sets the parameter set of the distribution.
  3129. * @param __param The new parameter set of the distribution.
  3130. */
  3131. void
  3132. param(const param_type& __param)
  3133. { _M_param = __param; }
  3134. /**
  3135. * @brief Returns the greatest lower bound value of the distribution.
  3136. */
  3137. result_type
  3138. min() const
  3139. { return std::numeric_limits<result_type>::min(); }
  3140. /**
  3141. * @brief Returns the least upper bound value of the distribution.
  3142. */
  3143. result_type
  3144. max() const
  3145. { return std::numeric_limits<result_type>::max(); }
  3146. /**
  3147. * @brief Generating functions.
  3148. */
  3149. template<typename _UniformRandomNumberGenerator>
  3150. result_type
  3151. operator()(_UniformRandomNumberGenerator& __urng)
  3152. { return this->operator()(__urng, _M_param); }
  3153. template<typename _UniformRandomNumberGenerator>
  3154. result_type
  3155. operator()(_UniformRandomNumberGenerator& __urng,
  3156. const param_type& __p)
  3157. {
  3158. __detail::_Adaptor<_UniformRandomNumberGenerator, double>
  3159. __aurng(__urng);
  3160. if ((__aurng() - __aurng.min())
  3161. < __p.p() * (__aurng.max() - __aurng.min()))
  3162. return true;
  3163. return false;
  3164. }
  3165. template<typename _ForwardIterator,
  3166. typename _UniformRandomNumberGenerator>
  3167. void
  3168. __generate(_ForwardIterator __f, _ForwardIterator __t,
  3169. _UniformRandomNumberGenerator& __urng)
  3170. { this->__generate(__f, __t, __urng, _M_param); }
  3171. template<typename _ForwardIterator,
  3172. typename _UniformRandomNumberGenerator>
  3173. void
  3174. __generate(_ForwardIterator __f, _ForwardIterator __t,
  3175. _UniformRandomNumberGenerator& __urng, const param_type& __p)
  3176. { this->__generate_impl(__f, __t, __urng, __p); }
  3177. template<typename _UniformRandomNumberGenerator>
  3178. void
  3179. __generate(result_type* __f, result_type* __t,
  3180. _UniformRandomNumberGenerator& __urng,
  3181. const param_type& __p)
  3182. { this->__generate_impl(__f, __t, __urng, __p); }
  3183. /**
  3184. * @brief Return true if two Bernoulli distributions have
  3185. * the same parameters.
  3186. */
  3187. friend bool
  3188. operator==(const bernoulli_distribution& __d1,
  3189. const bernoulli_distribution& __d2)
  3190. { return __d1._M_param == __d2._M_param; }
  3191. private:
  3192. template<typename _ForwardIterator,
  3193. typename _UniformRandomNumberGenerator>
  3194. void
  3195. __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
  3196. _UniformRandomNumberGenerator& __urng,
  3197. const param_type& __p);
  3198. param_type _M_param;
  3199. };
  3200. /**
  3201. * @brief Return true if two Bernoulli distributions have
  3202. * different parameters.
  3203. */
  3204. inline bool
  3205. operator!=(const std::bernoulli_distribution& __d1,
  3206. const std::bernoulli_distribution& __d2)
  3207. { return !(__d1 == __d2); }
  3208. /**
  3209. * @brief Inserts a %bernoulli_distribution random number distribution
  3210. * @p __x into the output stream @p __os.
  3211. *
  3212. * @param __os An output stream.
  3213. * @param __x A %bernoulli_distribution random number distribution.
  3214. *
  3215. * @returns The output stream with the state of @p __x inserted or in
  3216. * an error state.
  3217. */
  3218. template<typename _CharT, typename _Traits>
  3219. std::basic_ostream<_CharT, _Traits>&
  3220. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  3221. const std::bernoulli_distribution& __x);
  3222. /**
  3223. * @brief Extracts a %bernoulli_distribution random number distribution
  3224. * @p __x from the input stream @p __is.
  3225. *
  3226. * @param __is An input stream.
  3227. * @param __x A %bernoulli_distribution random number generator engine.
  3228. *
  3229. * @returns The input stream with @p __x extracted or in an error state.
  3230. */
  3231. template<typename _CharT, typename _Traits>
  3232. inline std::basic_istream<_CharT, _Traits>&
  3233. operator>>(std::basic_istream<_CharT, _Traits>& __is,
  3234. std::bernoulli_distribution& __x)
  3235. {
  3236. double __p;
  3237. if (__is >> __p)
  3238. __x.param(bernoulli_distribution::param_type(__p));
  3239. return __is;
  3240. }
  3241. /**
  3242. * @brief A discrete binomial random number distribution.
  3243. *
  3244. * The formula for the binomial probability density function is
  3245. * @f$p(i|t,p) = \binom{t}{i} p^i (1 - p)^{t - i}@f$ where @f$t@f$
  3246. * and @f$p@f$ are the parameters of the distribution.
  3247. */
  3248. template<typename _IntType = int>
  3249. class binomial_distribution
  3250. {
  3251. static_assert(std::is_integral<_IntType>::value,
  3252. "result_type must be an integral type");
  3253. public:
  3254. /** The type of the range of the distribution. */
  3255. typedef _IntType result_type;
  3256. /** Parameter type. */
  3257. struct param_type
  3258. {
  3259. typedef binomial_distribution<_IntType> distribution_type;
  3260. friend class binomial_distribution<_IntType>;
  3261. param_type() : param_type(1) { }
  3262. explicit
  3263. param_type(_IntType __t, double __p = 0.5)
  3264. : _M_t(__t), _M_p(__p)
  3265. {
  3266. __glibcxx_assert((_M_t >= _IntType(0))
  3267. && (_M_p >= 0.0)
  3268. && (_M_p <= 1.0));
  3269. _M_initialize();
  3270. }
  3271. _IntType
  3272. t() const
  3273. { return _M_t; }
  3274. double
  3275. p() const
  3276. { return _M_p; }
  3277. friend bool
  3278. operator==(const param_type& __p1, const param_type& __p2)
  3279. { return __p1._M_t == __p2._M_t && __p1._M_p == __p2._M_p; }
  3280. friend bool
  3281. operator!=(const param_type& __p1, const param_type& __p2)
  3282. { return !(__p1 == __p2); }
  3283. private:
  3284. void
  3285. _M_initialize();
  3286. _IntType _M_t;
  3287. double _M_p;
  3288. double _M_q;
  3289. #if _GLIBCXX_USE_C99_MATH_TR1
  3290. double _M_d1, _M_d2, _M_s1, _M_s2, _M_c,
  3291. _M_a1, _M_a123, _M_s, _M_lf, _M_lp1p;
  3292. #endif
  3293. bool _M_easy;
  3294. };
  3295. // constructors and member functions
  3296. binomial_distribution() : binomial_distribution(1) { }
  3297. explicit
  3298. binomial_distribution(_IntType __t, double __p = 0.5)
  3299. : _M_param(__t, __p), _M_nd()
  3300. { }
  3301. explicit
  3302. binomial_distribution(const param_type& __p)
  3303. : _M_param(__p), _M_nd()
  3304. { }
  3305. /**
  3306. * @brief Resets the distribution state.
  3307. */
  3308. void
  3309. reset()
  3310. { _M_nd.reset(); }
  3311. /**
  3312. * @brief Returns the distribution @p t parameter.
  3313. */
  3314. _IntType
  3315. t() const
  3316. { return _M_param.t(); }
  3317. /**
  3318. * @brief Returns the distribution @p p parameter.
  3319. */
  3320. double
  3321. p() const
  3322. { return _M_param.p(); }
  3323. /**
  3324. * @brief Returns the parameter set of the distribution.
  3325. */
  3326. param_type
  3327. param() const
  3328. { return _M_param; }
  3329. /**
  3330. * @brief Sets the parameter set of the distribution.
  3331. * @param __param The new parameter set of the distribution.
  3332. */
  3333. void
  3334. param(const param_type& __param)
  3335. { _M_param = __param; }
  3336. /**
  3337. * @brief Returns the greatest lower bound value of the distribution.
  3338. */
  3339. result_type
  3340. min() const
  3341. { return 0; }
  3342. /**
  3343. * @brief Returns the least upper bound value of the distribution.
  3344. */
  3345. result_type
  3346. max() const
  3347. { return _M_param.t(); }
  3348. /**
  3349. * @brief Generating functions.
  3350. */
  3351. template<typename _UniformRandomNumberGenerator>
  3352. result_type
  3353. operator()(_UniformRandomNumberGenerator& __urng)
  3354. { return this->operator()(__urng, _M_param); }
  3355. template<typename _UniformRandomNumberGenerator>
  3356. result_type
  3357. operator()(_UniformRandomNumberGenerator& __urng,
  3358. const param_type& __p);
  3359. template<typename _ForwardIterator,
  3360. typename _UniformRandomNumberGenerator>
  3361. void
  3362. __generate(_ForwardIterator __f, _ForwardIterator __t,
  3363. _UniformRandomNumberGenerator& __urng)
  3364. { this->__generate(__f, __t, __urng, _M_param); }
  3365. template<typename _ForwardIterator,
  3366. typename _UniformRandomNumberGenerator>
  3367. void
  3368. __generate(_ForwardIterator __f, _ForwardIterator __t,
  3369. _UniformRandomNumberGenerator& __urng,
  3370. const param_type& __p)
  3371. { this->__generate_impl(__f, __t, __urng, __p); }
  3372. template<typename _UniformRandomNumberGenerator>
  3373. void
  3374. __generate(result_type* __f, result_type* __t,
  3375. _UniformRandomNumberGenerator& __urng,
  3376. const param_type& __p)
  3377. { this->__generate_impl(__f, __t, __urng, __p); }
  3378. /**
  3379. * @brief Return true if two binomial distributions have
  3380. * the same parameters and the sequences that would
  3381. * be generated are equal.
  3382. */
  3383. friend bool
  3384. operator==(const binomial_distribution& __d1,
  3385. const binomial_distribution& __d2)
  3386. #ifdef _GLIBCXX_USE_C99_MATH_TR1
  3387. { return __d1._M_param == __d2._M_param && __d1._M_nd == __d2._M_nd; }
  3388. #else
  3389. { return __d1._M_param == __d2._M_param; }
  3390. #endif
  3391. /**
  3392. * @brief Inserts a %binomial_distribution random number distribution
  3393. * @p __x into the output stream @p __os.
  3394. *
  3395. * @param __os An output stream.
  3396. * @param __x A %binomial_distribution random number distribution.
  3397. *
  3398. * @returns The output stream with the state of @p __x inserted or in
  3399. * an error state.
  3400. */
  3401. template<typename _IntType1,
  3402. typename _CharT, typename _Traits>
  3403. friend std::basic_ostream<_CharT, _Traits>&
  3404. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  3405. const std::binomial_distribution<_IntType1>& __x);
  3406. /**
  3407. * @brief Extracts a %binomial_distribution random number distribution
  3408. * @p __x from the input stream @p __is.
  3409. *
  3410. * @param __is An input stream.
  3411. * @param __x A %binomial_distribution random number generator engine.
  3412. *
  3413. * @returns The input stream with @p __x extracted or in an error
  3414. * state.
  3415. */
  3416. template<typename _IntType1,
  3417. typename _CharT, typename _Traits>
  3418. friend std::basic_istream<_CharT, _Traits>&
  3419. operator>>(std::basic_istream<_CharT, _Traits>& __is,
  3420. std::binomial_distribution<_IntType1>& __x);
  3421. private:
  3422. template<typename _ForwardIterator,
  3423. typename _UniformRandomNumberGenerator>
  3424. void
  3425. __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
  3426. _UniformRandomNumberGenerator& __urng,
  3427. const param_type& __p);
  3428. template<typename _UniformRandomNumberGenerator>
  3429. result_type
  3430. _M_waiting(_UniformRandomNumberGenerator& __urng,
  3431. _IntType __t, double __q);
  3432. param_type _M_param;
  3433. // NB: Unused when _GLIBCXX_USE_C99_MATH_TR1 is undefined.
  3434. std::normal_distribution<double> _M_nd;
  3435. };
  3436. /**
  3437. * @brief Return true if two binomial distributions are different.
  3438. */
  3439. template<typename _IntType>
  3440. inline bool
  3441. operator!=(const std::binomial_distribution<_IntType>& __d1,
  3442. const std::binomial_distribution<_IntType>& __d2)
  3443. { return !(__d1 == __d2); }
  3444. /**
  3445. * @brief A discrete geometric random number distribution.
  3446. *
  3447. * The formula for the geometric probability density function is
  3448. * @f$p(i|p) = p(1 - p)^{i}@f$ where @f$p@f$ is the parameter of the
  3449. * distribution.
  3450. */
  3451. template<typename _IntType = int>
  3452. class geometric_distribution
  3453. {
  3454. static_assert(std::is_integral<_IntType>::value,
  3455. "result_type must be an integral type");
  3456. public:
  3457. /** The type of the range of the distribution. */
  3458. typedef _IntType result_type;
  3459. /** Parameter type. */
  3460. struct param_type
  3461. {
  3462. typedef geometric_distribution<_IntType> distribution_type;
  3463. friend class geometric_distribution<_IntType>;
  3464. param_type() : param_type(0.5) { }
  3465. explicit
  3466. param_type(double __p)
  3467. : _M_p(__p)
  3468. {
  3469. __glibcxx_assert((_M_p > 0.0) && (_M_p < 1.0));
  3470. _M_initialize();
  3471. }
  3472. double
  3473. p() const
  3474. { return _M_p; }
  3475. friend bool
  3476. operator==(const param_type& __p1, const param_type& __p2)
  3477. { return __p1._M_p == __p2._M_p; }
  3478. friend bool
  3479. operator!=(const param_type& __p1, const param_type& __p2)
  3480. { return !(__p1 == __p2); }
  3481. private:
  3482. void
  3483. _M_initialize()
  3484. { _M_log_1_p = std::log(1.0 - _M_p); }
  3485. double _M_p;
  3486. double _M_log_1_p;
  3487. };
  3488. // constructors and member functions
  3489. geometric_distribution() : geometric_distribution(0.5) { }
  3490. explicit
  3491. geometric_distribution(double __p)
  3492. : _M_param(__p)
  3493. { }
  3494. explicit
  3495. geometric_distribution(const param_type& __p)
  3496. : _M_param(__p)
  3497. { }
  3498. /**
  3499. * @brief Resets the distribution state.
  3500. *
  3501. * Does nothing for the geometric distribution.
  3502. */
  3503. void
  3504. reset() { }
  3505. /**
  3506. * @brief Returns the distribution parameter @p p.
  3507. */
  3508. double
  3509. p() const
  3510. { return _M_param.p(); }
  3511. /**
  3512. * @brief Returns the parameter set of the distribution.
  3513. */
  3514. param_type
  3515. param() const
  3516. { return _M_param; }
  3517. /**
  3518. * @brief Sets the parameter set of the distribution.
  3519. * @param __param The new parameter set of the distribution.
  3520. */
  3521. void
  3522. param(const param_type& __param)
  3523. { _M_param = __param; }
  3524. /**
  3525. * @brief Returns the greatest lower bound value of the distribution.
  3526. */
  3527. result_type
  3528. min() const
  3529. { return 0; }
  3530. /**
  3531. * @brief Returns the least upper bound value of the distribution.
  3532. */
  3533. result_type
  3534. max() const
  3535. { return std::numeric_limits<result_type>::max(); }
  3536. /**
  3537. * @brief Generating functions.
  3538. */
  3539. template<typename _UniformRandomNumberGenerator>
  3540. result_type
  3541. operator()(_UniformRandomNumberGenerator& __urng)
  3542. { return this->operator()(__urng, _M_param); }
  3543. template<typename _UniformRandomNumberGenerator>
  3544. result_type
  3545. operator()(_UniformRandomNumberGenerator& __urng,
  3546. const param_type& __p);
  3547. template<typename _ForwardIterator,
  3548. typename _UniformRandomNumberGenerator>
  3549. void
  3550. __generate(_ForwardIterator __f, _ForwardIterator __t,
  3551. _UniformRandomNumberGenerator& __urng)
  3552. { this->__generate(__f, __t, __urng, _M_param); }
  3553. template<typename _ForwardIterator,
  3554. typename _UniformRandomNumberGenerator>
  3555. void
  3556. __generate(_ForwardIterator __f, _ForwardIterator __t,
  3557. _UniformRandomNumberGenerator& __urng,
  3558. const param_type& __p)
  3559. { this->__generate_impl(__f, __t, __urng, __p); }
  3560. template<typename _UniformRandomNumberGenerator>
  3561. void
  3562. __generate(result_type* __f, result_type* __t,
  3563. _UniformRandomNumberGenerator& __urng,
  3564. const param_type& __p)
  3565. { this->__generate_impl(__f, __t, __urng, __p); }
  3566. /**
  3567. * @brief Return true if two geometric distributions have
  3568. * the same parameters.
  3569. */
  3570. friend bool
  3571. operator==(const geometric_distribution& __d1,
  3572. const geometric_distribution& __d2)
  3573. { return __d1._M_param == __d2._M_param; }
  3574. private:
  3575. template<typename _ForwardIterator,
  3576. typename _UniformRandomNumberGenerator>
  3577. void
  3578. __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
  3579. _UniformRandomNumberGenerator& __urng,
  3580. const param_type& __p);
  3581. param_type _M_param;
  3582. };
  3583. /**
  3584. * @brief Return true if two geometric distributions have
  3585. * different parameters.
  3586. */
  3587. template<typename _IntType>
  3588. inline bool
  3589. operator!=(const std::geometric_distribution<_IntType>& __d1,
  3590. const std::geometric_distribution<_IntType>& __d2)
  3591. { return !(__d1 == __d2); }
  3592. /**
  3593. * @brief Inserts a %geometric_distribution random number distribution
  3594. * @p __x into the output stream @p __os.
  3595. *
  3596. * @param __os An output stream.
  3597. * @param __x A %geometric_distribution random number distribution.
  3598. *
  3599. * @returns The output stream with the state of @p __x inserted or in
  3600. * an error state.
  3601. */
  3602. template<typename _IntType,
  3603. typename _CharT, typename _Traits>
  3604. std::basic_ostream<_CharT, _Traits>&
  3605. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  3606. const std::geometric_distribution<_IntType>& __x);
  3607. /**
  3608. * @brief Extracts a %geometric_distribution random number distribution
  3609. * @p __x from the input stream @p __is.
  3610. *
  3611. * @param __is An input stream.
  3612. * @param __x A %geometric_distribution random number generator engine.
  3613. *
  3614. * @returns The input stream with @p __x extracted or in an error state.
  3615. */
  3616. template<typename _IntType,
  3617. typename _CharT, typename _Traits>
  3618. std::basic_istream<_CharT, _Traits>&
  3619. operator>>(std::basic_istream<_CharT, _Traits>& __is,
  3620. std::geometric_distribution<_IntType>& __x);
  3621. /**
  3622. * @brief A negative_binomial_distribution random number distribution.
  3623. *
  3624. * The formula for the negative binomial probability mass function is
  3625. * @f$p(i) = \binom{n}{i} p^i (1 - p)^{t - i}@f$ where @f$t@f$
  3626. * and @f$p@f$ are the parameters of the distribution.
  3627. */
  3628. template<typename _IntType = int>
  3629. class negative_binomial_distribution
  3630. {
  3631. static_assert(std::is_integral<_IntType>::value,
  3632. "result_type must be an integral type");
  3633. public:
  3634. /** The type of the range of the distribution. */
  3635. typedef _IntType result_type;
  3636. /** Parameter type. */
  3637. struct param_type
  3638. {
  3639. typedef negative_binomial_distribution<_IntType> distribution_type;
  3640. param_type() : param_type(1) { }
  3641. explicit
  3642. param_type(_IntType __k, double __p = 0.5)
  3643. : _M_k(__k), _M_p(__p)
  3644. {
  3645. __glibcxx_assert((_M_k > 0) && (_M_p > 0.0) && (_M_p <= 1.0));
  3646. }
  3647. _IntType
  3648. k() const
  3649. { return _M_k; }
  3650. double
  3651. p() const
  3652. { return _M_p; }
  3653. friend bool
  3654. operator==(const param_type& __p1, const param_type& __p2)
  3655. { return __p1._M_k == __p2._M_k && __p1._M_p == __p2._M_p; }
  3656. friend bool
  3657. operator!=(const param_type& __p1, const param_type& __p2)
  3658. { return !(__p1 == __p2); }
  3659. private:
  3660. _IntType _M_k;
  3661. double _M_p;
  3662. };
  3663. negative_binomial_distribution() : negative_binomial_distribution(1) { }
  3664. explicit
  3665. negative_binomial_distribution(_IntType __k, double __p = 0.5)
  3666. : _M_param(__k, __p), _M_gd(__k, (1.0 - __p) / __p)
  3667. { }
  3668. explicit
  3669. negative_binomial_distribution(const param_type& __p)
  3670. : _M_param(__p), _M_gd(__p.k(), (1.0 - __p.p()) / __p.p())
  3671. { }
  3672. /**
  3673. * @brief Resets the distribution state.
  3674. */
  3675. void
  3676. reset()
  3677. { _M_gd.reset(); }
  3678. /**
  3679. * @brief Return the @f$k@f$ parameter of the distribution.
  3680. */
  3681. _IntType
  3682. k() const
  3683. { return _M_param.k(); }
  3684. /**
  3685. * @brief Return the @f$p@f$ parameter of the distribution.
  3686. */
  3687. double
  3688. p() const
  3689. { return _M_param.p(); }
  3690. /**
  3691. * @brief Returns the parameter set of the distribution.
  3692. */
  3693. param_type
  3694. param() const
  3695. { return _M_param; }
  3696. /**
  3697. * @brief Sets the parameter set of the distribution.
  3698. * @param __param The new parameter set of the distribution.
  3699. */
  3700. void
  3701. param(const param_type& __param)
  3702. { _M_param = __param; }
  3703. /**
  3704. * @brief Returns the greatest lower bound value of the distribution.
  3705. */
  3706. result_type
  3707. min() const
  3708. { return result_type(0); }
  3709. /**
  3710. * @brief Returns the least upper bound value of the distribution.
  3711. */
  3712. result_type
  3713. max() const
  3714. { return std::numeric_limits<result_type>::max(); }
  3715. /**
  3716. * @brief Generating functions.
  3717. */
  3718. template<typename _UniformRandomNumberGenerator>
  3719. result_type
  3720. operator()(_UniformRandomNumberGenerator& __urng);
  3721. template<typename _UniformRandomNumberGenerator>
  3722. result_type
  3723. operator()(_UniformRandomNumberGenerator& __urng,
  3724. const param_type& __p);
  3725. template<typename _ForwardIterator,
  3726. typename _UniformRandomNumberGenerator>
  3727. void
  3728. __generate(_ForwardIterator __f, _ForwardIterator __t,
  3729. _UniformRandomNumberGenerator& __urng)
  3730. { this->__generate_impl(__f, __t, __urng); }
  3731. template<typename _ForwardIterator,
  3732. typename _UniformRandomNumberGenerator>
  3733. void
  3734. __generate(_ForwardIterator __f, _ForwardIterator __t,
  3735. _UniformRandomNumberGenerator& __urng,
  3736. const param_type& __p)
  3737. { this->__generate_impl(__f, __t, __urng, __p); }
  3738. template<typename _UniformRandomNumberGenerator>
  3739. void
  3740. __generate(result_type* __f, result_type* __t,
  3741. _UniformRandomNumberGenerator& __urng)
  3742. { this->__generate_impl(__f, __t, __urng); }
  3743. template<typename _UniformRandomNumberGenerator>
  3744. void
  3745. __generate(result_type* __f, result_type* __t,
  3746. _UniformRandomNumberGenerator& __urng,
  3747. const param_type& __p)
  3748. { this->__generate_impl(__f, __t, __urng, __p); }
  3749. /**
  3750. * @brief Return true if two negative binomial distributions have
  3751. * the same parameters and the sequences that would be
  3752. * generated are equal.
  3753. */
  3754. friend bool
  3755. operator==(const negative_binomial_distribution& __d1,
  3756. const negative_binomial_distribution& __d2)
  3757. { return __d1._M_param == __d2._M_param && __d1._M_gd == __d2._M_gd; }
  3758. /**
  3759. * @brief Inserts a %negative_binomial_distribution random
  3760. * number distribution @p __x into the output stream @p __os.
  3761. *
  3762. * @param __os An output stream.
  3763. * @param __x A %negative_binomial_distribution random number
  3764. * distribution.
  3765. *
  3766. * @returns The output stream with the state of @p __x inserted or in
  3767. * an error state.
  3768. */
  3769. template<typename _IntType1, typename _CharT, typename _Traits>
  3770. friend std::basic_ostream<_CharT, _Traits>&
  3771. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  3772. const std::negative_binomial_distribution<_IntType1>& __x);
  3773. /**
  3774. * @brief Extracts a %negative_binomial_distribution random number
  3775. * distribution @p __x from the input stream @p __is.
  3776. *
  3777. * @param __is An input stream.
  3778. * @param __x A %negative_binomial_distribution random number
  3779. * generator engine.
  3780. *
  3781. * @returns The input stream with @p __x extracted or in an error state.
  3782. */
  3783. template<typename _IntType1, typename _CharT, typename _Traits>
  3784. friend std::basic_istream<_CharT, _Traits>&
  3785. operator>>(std::basic_istream<_CharT, _Traits>& __is,
  3786. std::negative_binomial_distribution<_IntType1>& __x);
  3787. private:
  3788. template<typename _ForwardIterator,
  3789. typename _UniformRandomNumberGenerator>
  3790. void
  3791. __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
  3792. _UniformRandomNumberGenerator& __urng);
  3793. template<typename _ForwardIterator,
  3794. typename _UniformRandomNumberGenerator>
  3795. void
  3796. __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
  3797. _UniformRandomNumberGenerator& __urng,
  3798. const param_type& __p);
  3799. param_type _M_param;
  3800. std::gamma_distribution<double> _M_gd;
  3801. };
  3802. /**
  3803. * @brief Return true if two negative binomial distributions are different.
  3804. */
  3805. template<typename _IntType>
  3806. inline bool
  3807. operator!=(const std::negative_binomial_distribution<_IntType>& __d1,
  3808. const std::negative_binomial_distribution<_IntType>& __d2)
  3809. { return !(__d1 == __d2); }
  3810. /// @} group random_distributions_bernoulli
  3811. /**
  3812. * @addtogroup random_distributions_poisson Poisson Distributions
  3813. * @ingroup random_distributions
  3814. * @{
  3815. */
  3816. /**
  3817. * @brief A discrete Poisson random number distribution.
  3818. *
  3819. * The formula for the Poisson probability density function is
  3820. * @f$p(i|\mu) = \frac{\mu^i}{i!} e^{-\mu}@f$ where @f$\mu@f$ is the
  3821. * parameter of the distribution.
  3822. */
  3823. template<typename _IntType = int>
  3824. class poisson_distribution
  3825. {
  3826. static_assert(std::is_integral<_IntType>::value,
  3827. "result_type must be an integral type");
  3828. public:
  3829. /** The type of the range of the distribution. */
  3830. typedef _IntType result_type;
  3831. /** Parameter type. */
  3832. struct param_type
  3833. {
  3834. typedef poisson_distribution<_IntType> distribution_type;
  3835. friend class poisson_distribution<_IntType>;
  3836. param_type() : param_type(1.0) { }
  3837. explicit
  3838. param_type(double __mean)
  3839. : _M_mean(__mean)
  3840. {
  3841. __glibcxx_assert(_M_mean > 0.0);
  3842. _M_initialize();
  3843. }
  3844. double
  3845. mean() const
  3846. { return _M_mean; }
  3847. friend bool
  3848. operator==(const param_type& __p1, const param_type& __p2)
  3849. { return __p1._M_mean == __p2._M_mean; }
  3850. friend bool
  3851. operator!=(const param_type& __p1, const param_type& __p2)
  3852. { return !(__p1 == __p2); }
  3853. private:
  3854. // Hosts either log(mean) or the threshold of the simple method.
  3855. void
  3856. _M_initialize();
  3857. double _M_mean;
  3858. double _M_lm_thr;
  3859. #if _GLIBCXX_USE_C99_MATH_TR1
  3860. double _M_lfm, _M_sm, _M_d, _M_scx, _M_1cx, _M_c2b, _M_cb;
  3861. #endif
  3862. };
  3863. // constructors and member functions
  3864. poisson_distribution() : poisson_distribution(1.0) { }
  3865. explicit
  3866. poisson_distribution(double __mean)
  3867. : _M_param(__mean), _M_nd()
  3868. { }
  3869. explicit
  3870. poisson_distribution(const param_type& __p)
  3871. : _M_param(__p), _M_nd()
  3872. { }
  3873. /**
  3874. * @brief Resets the distribution state.
  3875. */
  3876. void
  3877. reset()
  3878. { _M_nd.reset(); }
  3879. /**
  3880. * @brief Returns the distribution parameter @p mean.
  3881. */
  3882. double
  3883. mean() const
  3884. { return _M_param.mean(); }
  3885. /**
  3886. * @brief Returns the parameter set of the distribution.
  3887. */
  3888. param_type
  3889. param() const
  3890. { return _M_param; }
  3891. /**
  3892. * @brief Sets the parameter set of the distribution.
  3893. * @param __param The new parameter set of the distribution.
  3894. */
  3895. void
  3896. param(const param_type& __param)
  3897. { _M_param = __param; }
  3898. /**
  3899. * @brief Returns the greatest lower bound value of the distribution.
  3900. */
  3901. result_type
  3902. min() const
  3903. { return 0; }
  3904. /**
  3905. * @brief Returns the least upper bound value of the distribution.
  3906. */
  3907. result_type
  3908. max() const
  3909. { return std::numeric_limits<result_type>::max(); }
  3910. /**
  3911. * @brief Generating functions.
  3912. */
  3913. template<typename _UniformRandomNumberGenerator>
  3914. result_type
  3915. operator()(_UniformRandomNumberGenerator& __urng)
  3916. { return this->operator()(__urng, _M_param); }
  3917. template<typename _UniformRandomNumberGenerator>
  3918. result_type
  3919. operator()(_UniformRandomNumberGenerator& __urng,
  3920. const param_type& __p);
  3921. template<typename _ForwardIterator,
  3922. typename _UniformRandomNumberGenerator>
  3923. void
  3924. __generate(_ForwardIterator __f, _ForwardIterator __t,
  3925. _UniformRandomNumberGenerator& __urng)
  3926. { this->__generate(__f, __t, __urng, _M_param); }
  3927. template<typename _ForwardIterator,
  3928. typename _UniformRandomNumberGenerator>
  3929. void
  3930. __generate(_ForwardIterator __f, _ForwardIterator __t,
  3931. _UniformRandomNumberGenerator& __urng,
  3932. const param_type& __p)
  3933. { this->__generate_impl(__f, __t, __urng, __p); }
  3934. template<typename _UniformRandomNumberGenerator>
  3935. void
  3936. __generate(result_type* __f, result_type* __t,
  3937. _UniformRandomNumberGenerator& __urng,
  3938. const param_type& __p)
  3939. { this->__generate_impl(__f, __t, __urng, __p); }
  3940. /**
  3941. * @brief Return true if two Poisson distributions have the same
  3942. * parameters and the sequences that would be generated
  3943. * are equal.
  3944. */
  3945. friend bool
  3946. operator==(const poisson_distribution& __d1,
  3947. const poisson_distribution& __d2)
  3948. #ifdef _GLIBCXX_USE_C99_MATH_TR1
  3949. { return __d1._M_param == __d2._M_param && __d1._M_nd == __d2._M_nd; }
  3950. #else
  3951. { return __d1._M_param == __d2._M_param; }
  3952. #endif
  3953. /**
  3954. * @brief Inserts a %poisson_distribution random number distribution
  3955. * @p __x into the output stream @p __os.
  3956. *
  3957. * @param __os An output stream.
  3958. * @param __x A %poisson_distribution random number distribution.
  3959. *
  3960. * @returns The output stream with the state of @p __x inserted or in
  3961. * an error state.
  3962. */
  3963. template<typename _IntType1, typename _CharT, typename _Traits>
  3964. friend std::basic_ostream<_CharT, _Traits>&
  3965. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  3966. const std::poisson_distribution<_IntType1>& __x);
  3967. /**
  3968. * @brief Extracts a %poisson_distribution random number distribution
  3969. * @p __x from the input stream @p __is.
  3970. *
  3971. * @param __is An input stream.
  3972. * @param __x A %poisson_distribution random number generator engine.
  3973. *
  3974. * @returns The input stream with @p __x extracted or in an error
  3975. * state.
  3976. */
  3977. template<typename _IntType1, typename _CharT, typename _Traits>
  3978. friend std::basic_istream<_CharT, _Traits>&
  3979. operator>>(std::basic_istream<_CharT, _Traits>& __is,
  3980. std::poisson_distribution<_IntType1>& __x);
  3981. private:
  3982. template<typename _ForwardIterator,
  3983. typename _UniformRandomNumberGenerator>
  3984. void
  3985. __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
  3986. _UniformRandomNumberGenerator& __urng,
  3987. const param_type& __p);
  3988. param_type _M_param;
  3989. // NB: Unused when _GLIBCXX_USE_C99_MATH_TR1 is undefined.
  3990. std::normal_distribution<double> _M_nd;
  3991. };
  3992. /**
  3993. * @brief Return true if two Poisson distributions are different.
  3994. */
  3995. template<typename _IntType>
  3996. inline bool
  3997. operator!=(const std::poisson_distribution<_IntType>& __d1,
  3998. const std::poisson_distribution<_IntType>& __d2)
  3999. { return !(__d1 == __d2); }
  4000. /**
  4001. * @brief An exponential continuous distribution for random numbers.
  4002. *
  4003. * The formula for the exponential probability density function is
  4004. * @f$p(x|\lambda) = \lambda e^{-\lambda x}@f$.
  4005. *
  4006. * <table border=1 cellpadding=10 cellspacing=0>
  4007. * <caption align=top>Distribution Statistics</caption>
  4008. * <tr><td>Mean</td><td>@f$\frac{1}{\lambda}@f$</td></tr>
  4009. * <tr><td>Median</td><td>@f$\frac{\ln 2}{\lambda}@f$</td></tr>
  4010. * <tr><td>Mode</td><td>@f$zero@f$</td></tr>
  4011. * <tr><td>Range</td><td>@f$[0, \infty]@f$</td></tr>
  4012. * <tr><td>Standard Deviation</td><td>@f$\frac{1}{\lambda}@f$</td></tr>
  4013. * </table>
  4014. */
  4015. template<typename _RealType = double>
  4016. class exponential_distribution
  4017. {
  4018. static_assert(std::is_floating_point<_RealType>::value,
  4019. "result_type must be a floating point type");
  4020. public:
  4021. /** The type of the range of the distribution. */
  4022. typedef _RealType result_type;
  4023. /** Parameter type. */
  4024. struct param_type
  4025. {
  4026. typedef exponential_distribution<_RealType> distribution_type;
  4027. param_type() : param_type(1.0) { }
  4028. explicit
  4029. param_type(_RealType __lambda)
  4030. : _M_lambda(__lambda)
  4031. {
  4032. __glibcxx_assert(_M_lambda > _RealType(0));
  4033. }
  4034. _RealType
  4035. lambda() const
  4036. { return _M_lambda; }
  4037. friend bool
  4038. operator==(const param_type& __p1, const param_type& __p2)
  4039. { return __p1._M_lambda == __p2._M_lambda; }
  4040. friend bool
  4041. operator!=(const param_type& __p1, const param_type& __p2)
  4042. { return !(__p1 == __p2); }
  4043. private:
  4044. _RealType _M_lambda;
  4045. };
  4046. public:
  4047. /**
  4048. * @brief Constructs an exponential distribution with inverse scale
  4049. * parameter 1.0
  4050. */
  4051. exponential_distribution() : exponential_distribution(1.0) { }
  4052. /**
  4053. * @brief Constructs an exponential distribution with inverse scale
  4054. * parameter @f$\lambda@f$.
  4055. */
  4056. explicit
  4057. exponential_distribution(_RealType __lambda)
  4058. : _M_param(__lambda)
  4059. { }
  4060. explicit
  4061. exponential_distribution(const param_type& __p)
  4062. : _M_param(__p)
  4063. { }
  4064. /**
  4065. * @brief Resets the distribution state.
  4066. *
  4067. * Has no effect on exponential distributions.
  4068. */
  4069. void
  4070. reset() { }
  4071. /**
  4072. * @brief Returns the inverse scale parameter of the distribution.
  4073. */
  4074. _RealType
  4075. lambda() const
  4076. { return _M_param.lambda(); }
  4077. /**
  4078. * @brief Returns the parameter set of the distribution.
  4079. */
  4080. param_type
  4081. param() const
  4082. { return _M_param; }
  4083. /**
  4084. * @brief Sets the parameter set of the distribution.
  4085. * @param __param The new parameter set of the distribution.
  4086. */
  4087. void
  4088. param(const param_type& __param)
  4089. { _M_param = __param; }
  4090. /**
  4091. * @brief Returns the greatest lower bound value of the distribution.
  4092. */
  4093. result_type
  4094. min() const
  4095. { return result_type(0); }
  4096. /**
  4097. * @brief Returns the least upper bound value of the distribution.
  4098. */
  4099. result_type
  4100. max() const
  4101. { return std::numeric_limits<result_type>::max(); }
  4102. /**
  4103. * @brief Generating functions.
  4104. */
  4105. template<typename _UniformRandomNumberGenerator>
  4106. result_type
  4107. operator()(_UniformRandomNumberGenerator& __urng)
  4108. { return this->operator()(__urng, _M_param); }
  4109. template<typename _UniformRandomNumberGenerator>
  4110. result_type
  4111. operator()(_UniformRandomNumberGenerator& __urng,
  4112. const param_type& __p)
  4113. {
  4114. __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
  4115. __aurng(__urng);
  4116. return -std::log(result_type(1) - __aurng()) / __p.lambda();
  4117. }
  4118. template<typename _ForwardIterator,
  4119. typename _UniformRandomNumberGenerator>
  4120. void
  4121. __generate(_ForwardIterator __f, _ForwardIterator __t,
  4122. _UniformRandomNumberGenerator& __urng)
  4123. { this->__generate(__f, __t, __urng, _M_param); }
  4124. template<typename _ForwardIterator,
  4125. typename _UniformRandomNumberGenerator>
  4126. void
  4127. __generate(_ForwardIterator __f, _ForwardIterator __t,
  4128. _UniformRandomNumberGenerator& __urng,
  4129. const param_type& __p)
  4130. { this->__generate_impl(__f, __t, __urng, __p); }
  4131. template<typename _UniformRandomNumberGenerator>
  4132. void
  4133. __generate(result_type* __f, result_type* __t,
  4134. _UniformRandomNumberGenerator& __urng,
  4135. const param_type& __p)
  4136. { this->__generate_impl(__f, __t, __urng, __p); }
  4137. /**
  4138. * @brief Return true if two exponential distributions have the same
  4139. * parameters.
  4140. */
  4141. friend bool
  4142. operator==(const exponential_distribution& __d1,
  4143. const exponential_distribution& __d2)
  4144. { return __d1._M_param == __d2._M_param; }
  4145. private:
  4146. template<typename _ForwardIterator,
  4147. typename _UniformRandomNumberGenerator>
  4148. void
  4149. __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
  4150. _UniformRandomNumberGenerator& __urng,
  4151. const param_type& __p);
  4152. param_type _M_param;
  4153. };
  4154. /**
  4155. * @brief Return true if two exponential distributions have different
  4156. * parameters.
  4157. */
  4158. template<typename _RealType>
  4159. inline bool
  4160. operator!=(const std::exponential_distribution<_RealType>& __d1,
  4161. const std::exponential_distribution<_RealType>& __d2)
  4162. { return !(__d1 == __d2); }
  4163. /**
  4164. * @brief Inserts a %exponential_distribution random number distribution
  4165. * @p __x into the output stream @p __os.
  4166. *
  4167. * @param __os An output stream.
  4168. * @param __x A %exponential_distribution random number distribution.
  4169. *
  4170. * @returns The output stream with the state of @p __x inserted or in
  4171. * an error state.
  4172. */
  4173. template<typename _RealType, typename _CharT, typename _Traits>
  4174. std::basic_ostream<_CharT, _Traits>&
  4175. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  4176. const std::exponential_distribution<_RealType>& __x);
  4177. /**
  4178. * @brief Extracts a %exponential_distribution random number distribution
  4179. * @p __x from the input stream @p __is.
  4180. *
  4181. * @param __is An input stream.
  4182. * @param __x A %exponential_distribution random number
  4183. * generator engine.
  4184. *
  4185. * @returns The input stream with @p __x extracted or in an error state.
  4186. */
  4187. template<typename _RealType, typename _CharT, typename _Traits>
  4188. std::basic_istream<_CharT, _Traits>&
  4189. operator>>(std::basic_istream<_CharT, _Traits>& __is,
  4190. std::exponential_distribution<_RealType>& __x);
  4191. /**
  4192. * @brief A weibull_distribution random number distribution.
  4193. *
  4194. * The formula for the normal probability density function is:
  4195. * @f[
  4196. * p(x|\alpha,\beta) = \frac{\alpha}{\beta} (\frac{x}{\beta})^{\alpha-1}
  4197. * \exp{(-(\frac{x}{\beta})^\alpha)}
  4198. * @f]
  4199. */
  4200. template<typename _RealType = double>
  4201. class weibull_distribution
  4202. {
  4203. static_assert(std::is_floating_point<_RealType>::value,
  4204. "result_type must be a floating point type");
  4205. public:
  4206. /** The type of the range of the distribution. */
  4207. typedef _RealType result_type;
  4208. /** Parameter type. */
  4209. struct param_type
  4210. {
  4211. typedef weibull_distribution<_RealType> distribution_type;
  4212. param_type() : param_type(1.0) { }
  4213. explicit
  4214. param_type(_RealType __a, _RealType __b = _RealType(1.0))
  4215. : _M_a(__a), _M_b(__b)
  4216. { }
  4217. _RealType
  4218. a() const
  4219. { return _M_a; }
  4220. _RealType
  4221. b() const
  4222. { return _M_b; }
  4223. friend bool
  4224. operator==(const param_type& __p1, const param_type& __p2)
  4225. { return __p1._M_a == __p2._M_a && __p1._M_b == __p2._M_b; }
  4226. friend bool
  4227. operator!=(const param_type& __p1, const param_type& __p2)
  4228. { return !(__p1 == __p2); }
  4229. private:
  4230. _RealType _M_a;
  4231. _RealType _M_b;
  4232. };
  4233. weibull_distribution() : weibull_distribution(1.0) { }
  4234. explicit
  4235. weibull_distribution(_RealType __a, _RealType __b = _RealType(1))
  4236. : _M_param(__a, __b)
  4237. { }
  4238. explicit
  4239. weibull_distribution(const param_type& __p)
  4240. : _M_param(__p)
  4241. { }
  4242. /**
  4243. * @brief Resets the distribution state.
  4244. */
  4245. void
  4246. reset()
  4247. { }
  4248. /**
  4249. * @brief Return the @f$a@f$ parameter of the distribution.
  4250. */
  4251. _RealType
  4252. a() const
  4253. { return _M_param.a(); }
  4254. /**
  4255. * @brief Return the @f$b@f$ parameter of the distribution.
  4256. */
  4257. _RealType
  4258. b() const
  4259. { return _M_param.b(); }
  4260. /**
  4261. * @brief Returns the parameter set of the distribution.
  4262. */
  4263. param_type
  4264. param() const
  4265. { return _M_param; }
  4266. /**
  4267. * @brief Sets the parameter set of the distribution.
  4268. * @param __param The new parameter set of the distribution.
  4269. */
  4270. void
  4271. param(const param_type& __param)
  4272. { _M_param = __param; }
  4273. /**
  4274. * @brief Returns the greatest lower bound value of the distribution.
  4275. */
  4276. result_type
  4277. min() const
  4278. { return result_type(0); }
  4279. /**
  4280. * @brief Returns the least upper bound value of the distribution.
  4281. */
  4282. result_type
  4283. max() const
  4284. { return std::numeric_limits<result_type>::max(); }
  4285. /**
  4286. * @brief Generating functions.
  4287. */
  4288. template<typename _UniformRandomNumberGenerator>
  4289. result_type
  4290. operator()(_UniformRandomNumberGenerator& __urng)
  4291. { return this->operator()(__urng, _M_param); }
  4292. template<typename _UniformRandomNumberGenerator>
  4293. result_type
  4294. operator()(_UniformRandomNumberGenerator& __urng,
  4295. const param_type& __p);
  4296. template<typename _ForwardIterator,
  4297. typename _UniformRandomNumberGenerator>
  4298. void
  4299. __generate(_ForwardIterator __f, _ForwardIterator __t,
  4300. _UniformRandomNumberGenerator& __urng)
  4301. { this->__generate(__f, __t, __urng, _M_param); }
  4302. template<typename _ForwardIterator,
  4303. typename _UniformRandomNumberGenerator>
  4304. void
  4305. __generate(_ForwardIterator __f, _ForwardIterator __t,
  4306. _UniformRandomNumberGenerator& __urng,
  4307. const param_type& __p)
  4308. { this->__generate_impl(__f, __t, __urng, __p); }
  4309. template<typename _UniformRandomNumberGenerator>
  4310. void
  4311. __generate(result_type* __f, result_type* __t,
  4312. _UniformRandomNumberGenerator& __urng,
  4313. const param_type& __p)
  4314. { this->__generate_impl(__f, __t, __urng, __p); }
  4315. /**
  4316. * @brief Return true if two Weibull distributions have the same
  4317. * parameters.
  4318. */
  4319. friend bool
  4320. operator==(const weibull_distribution& __d1,
  4321. const weibull_distribution& __d2)
  4322. { return __d1._M_param == __d2._M_param; }
  4323. private:
  4324. template<typename _ForwardIterator,
  4325. typename _UniformRandomNumberGenerator>
  4326. void
  4327. __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
  4328. _UniformRandomNumberGenerator& __urng,
  4329. const param_type& __p);
  4330. param_type _M_param;
  4331. };
  4332. /**
  4333. * @brief Return true if two Weibull distributions have different
  4334. * parameters.
  4335. */
  4336. template<typename _RealType>
  4337. inline bool
  4338. operator!=(const std::weibull_distribution<_RealType>& __d1,
  4339. const std::weibull_distribution<_RealType>& __d2)
  4340. { return !(__d1 == __d2); }
  4341. /**
  4342. * @brief Inserts a %weibull_distribution random number distribution
  4343. * @p __x into the output stream @p __os.
  4344. *
  4345. * @param __os An output stream.
  4346. * @param __x A %weibull_distribution random number distribution.
  4347. *
  4348. * @returns The output stream with the state of @p __x inserted or in
  4349. * an error state.
  4350. */
  4351. template<typename _RealType, typename _CharT, typename _Traits>
  4352. std::basic_ostream<_CharT, _Traits>&
  4353. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  4354. const std::weibull_distribution<_RealType>& __x);
  4355. /**
  4356. * @brief Extracts a %weibull_distribution random number distribution
  4357. * @p __x from the input stream @p __is.
  4358. *
  4359. * @param __is An input stream.
  4360. * @param __x A %weibull_distribution random number
  4361. * generator engine.
  4362. *
  4363. * @returns The input stream with @p __x extracted or in an error state.
  4364. */
  4365. template<typename _RealType, typename _CharT, typename _Traits>
  4366. std::basic_istream<_CharT, _Traits>&
  4367. operator>>(std::basic_istream<_CharT, _Traits>& __is,
  4368. std::weibull_distribution<_RealType>& __x);
  4369. /**
  4370. * @brief A extreme_value_distribution random number distribution.
  4371. *
  4372. * The formula for the normal probability mass function is
  4373. * @f[
  4374. * p(x|a,b) = \frac{1}{b}
  4375. * \exp( \frac{a-x}{b} - \exp(\frac{a-x}{b}))
  4376. * @f]
  4377. */
  4378. template<typename _RealType = double>
  4379. class extreme_value_distribution
  4380. {
  4381. static_assert(std::is_floating_point<_RealType>::value,
  4382. "result_type must be a floating point type");
  4383. public:
  4384. /** The type of the range of the distribution. */
  4385. typedef _RealType result_type;
  4386. /** Parameter type. */
  4387. struct param_type
  4388. {
  4389. typedef extreme_value_distribution<_RealType> distribution_type;
  4390. param_type() : param_type(0.0) { }
  4391. explicit
  4392. param_type(_RealType __a, _RealType __b = _RealType(1.0))
  4393. : _M_a(__a), _M_b(__b)
  4394. { }
  4395. _RealType
  4396. a() const
  4397. { return _M_a; }
  4398. _RealType
  4399. b() const
  4400. { return _M_b; }
  4401. friend bool
  4402. operator==(const param_type& __p1, const param_type& __p2)
  4403. { return __p1._M_a == __p2._M_a && __p1._M_b == __p2._M_b; }
  4404. friend bool
  4405. operator!=(const param_type& __p1, const param_type& __p2)
  4406. { return !(__p1 == __p2); }
  4407. private:
  4408. _RealType _M_a;
  4409. _RealType _M_b;
  4410. };
  4411. extreme_value_distribution() : extreme_value_distribution(0.0) { }
  4412. explicit
  4413. extreme_value_distribution(_RealType __a, _RealType __b = _RealType(1))
  4414. : _M_param(__a, __b)
  4415. { }
  4416. explicit
  4417. extreme_value_distribution(const param_type& __p)
  4418. : _M_param(__p)
  4419. { }
  4420. /**
  4421. * @brief Resets the distribution state.
  4422. */
  4423. void
  4424. reset()
  4425. { }
  4426. /**
  4427. * @brief Return the @f$a@f$ parameter of the distribution.
  4428. */
  4429. _RealType
  4430. a() const
  4431. { return _M_param.a(); }
  4432. /**
  4433. * @brief Return the @f$b@f$ parameter of the distribution.
  4434. */
  4435. _RealType
  4436. b() const
  4437. { return _M_param.b(); }
  4438. /**
  4439. * @brief Returns the parameter set of the distribution.
  4440. */
  4441. param_type
  4442. param() const
  4443. { return _M_param; }
  4444. /**
  4445. * @brief Sets the parameter set of the distribution.
  4446. * @param __param The new parameter set of the distribution.
  4447. */
  4448. void
  4449. param(const param_type& __param)
  4450. { _M_param = __param; }
  4451. /**
  4452. * @brief Returns the greatest lower bound value of the distribution.
  4453. */
  4454. result_type
  4455. min() const
  4456. { return std::numeric_limits<result_type>::lowest(); }
  4457. /**
  4458. * @brief Returns the least upper bound value of the distribution.
  4459. */
  4460. result_type
  4461. max() const
  4462. { return std::numeric_limits<result_type>::max(); }
  4463. /**
  4464. * @brief Generating functions.
  4465. */
  4466. template<typename _UniformRandomNumberGenerator>
  4467. result_type
  4468. operator()(_UniformRandomNumberGenerator& __urng)
  4469. { return this->operator()(__urng, _M_param); }
  4470. template<typename _UniformRandomNumberGenerator>
  4471. result_type
  4472. operator()(_UniformRandomNumberGenerator& __urng,
  4473. const param_type& __p);
  4474. template<typename _ForwardIterator,
  4475. typename _UniformRandomNumberGenerator>
  4476. void
  4477. __generate(_ForwardIterator __f, _ForwardIterator __t,
  4478. _UniformRandomNumberGenerator& __urng)
  4479. { this->__generate(__f, __t, __urng, _M_param); }
  4480. template<typename _ForwardIterator,
  4481. typename _UniformRandomNumberGenerator>
  4482. void
  4483. __generate(_ForwardIterator __f, _ForwardIterator __t,
  4484. _UniformRandomNumberGenerator& __urng,
  4485. const param_type& __p)
  4486. { this->__generate_impl(__f, __t, __urng, __p); }
  4487. template<typename _UniformRandomNumberGenerator>
  4488. void
  4489. __generate(result_type* __f, result_type* __t,
  4490. _UniformRandomNumberGenerator& __urng,
  4491. const param_type& __p)
  4492. { this->__generate_impl(__f, __t, __urng, __p); }
  4493. /**
  4494. * @brief Return true if two extreme value distributions have the same
  4495. * parameters.
  4496. */
  4497. friend bool
  4498. operator==(const extreme_value_distribution& __d1,
  4499. const extreme_value_distribution& __d2)
  4500. { return __d1._M_param == __d2._M_param; }
  4501. private:
  4502. template<typename _ForwardIterator,
  4503. typename _UniformRandomNumberGenerator>
  4504. void
  4505. __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
  4506. _UniformRandomNumberGenerator& __urng,
  4507. const param_type& __p);
  4508. param_type _M_param;
  4509. };
  4510. /**
  4511. * @brief Return true if two extreme value distributions have different
  4512. * parameters.
  4513. */
  4514. template<typename _RealType>
  4515. inline bool
  4516. operator!=(const std::extreme_value_distribution<_RealType>& __d1,
  4517. const std::extreme_value_distribution<_RealType>& __d2)
  4518. { return !(__d1 == __d2); }
  4519. /**
  4520. * @brief Inserts a %extreme_value_distribution random number distribution
  4521. * @p __x into the output stream @p __os.
  4522. *
  4523. * @param __os An output stream.
  4524. * @param __x A %extreme_value_distribution random number distribution.
  4525. *
  4526. * @returns The output stream with the state of @p __x inserted or in
  4527. * an error state.
  4528. */
  4529. template<typename _RealType, typename _CharT, typename _Traits>
  4530. std::basic_ostream<_CharT, _Traits>&
  4531. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  4532. const std::extreme_value_distribution<_RealType>& __x);
  4533. /**
  4534. * @brief Extracts a %extreme_value_distribution random number
  4535. * distribution @p __x from the input stream @p __is.
  4536. *
  4537. * @param __is An input stream.
  4538. * @param __x A %extreme_value_distribution random number
  4539. * generator engine.
  4540. *
  4541. * @returns The input stream with @p __x extracted or in an error state.
  4542. */
  4543. template<typename _RealType, typename _CharT, typename _Traits>
  4544. std::basic_istream<_CharT, _Traits>&
  4545. operator>>(std::basic_istream<_CharT, _Traits>& __is,
  4546. std::extreme_value_distribution<_RealType>& __x);
  4547. /**
  4548. * @brief A discrete_distribution random number distribution.
  4549. *
  4550. * The formula for the discrete probability mass function is
  4551. *
  4552. */
  4553. template<typename _IntType = int>
  4554. class discrete_distribution
  4555. {
  4556. static_assert(std::is_integral<_IntType>::value,
  4557. "result_type must be an integral type");
  4558. public:
  4559. /** The type of the range of the distribution. */
  4560. typedef _IntType result_type;
  4561. /** Parameter type. */
  4562. struct param_type
  4563. {
  4564. typedef discrete_distribution<_IntType> distribution_type;
  4565. friend class discrete_distribution<_IntType>;
  4566. param_type()
  4567. : _M_prob(), _M_cp()
  4568. { }
  4569. template<typename _InputIterator>
  4570. param_type(_InputIterator __wbegin,
  4571. _InputIterator __wend)
  4572. : _M_prob(__wbegin, __wend), _M_cp()
  4573. { _M_initialize(); }
  4574. param_type(initializer_list<double> __wil)
  4575. : _M_prob(__wil.begin(), __wil.end()), _M_cp()
  4576. { _M_initialize(); }
  4577. template<typename _Func>
  4578. param_type(size_t __nw, double __xmin, double __xmax,
  4579. _Func __fw);
  4580. // See: http://cpp-next.com/archive/2010/10/implicit-move-must-go/
  4581. param_type(const param_type&) = default;
  4582. param_type& operator=(const param_type&) = default;
  4583. std::vector<double>
  4584. probabilities() const
  4585. { return _M_prob.empty() ? std::vector<double>(1, 1.0) : _M_prob; }
  4586. friend bool
  4587. operator==(const param_type& __p1, const param_type& __p2)
  4588. { return __p1._M_prob == __p2._M_prob; }
  4589. friend bool
  4590. operator!=(const param_type& __p1, const param_type& __p2)
  4591. { return !(__p1 == __p2); }
  4592. private:
  4593. void
  4594. _M_initialize();
  4595. std::vector<double> _M_prob;
  4596. std::vector<double> _M_cp;
  4597. };
  4598. discrete_distribution()
  4599. : _M_param()
  4600. { }
  4601. template<typename _InputIterator>
  4602. discrete_distribution(_InputIterator __wbegin,
  4603. _InputIterator __wend)
  4604. : _M_param(__wbegin, __wend)
  4605. { }
  4606. discrete_distribution(initializer_list<double> __wl)
  4607. : _M_param(__wl)
  4608. { }
  4609. template<typename _Func>
  4610. discrete_distribution(size_t __nw, double __xmin, double __xmax,
  4611. _Func __fw)
  4612. : _M_param(__nw, __xmin, __xmax, __fw)
  4613. { }
  4614. explicit
  4615. discrete_distribution(const param_type& __p)
  4616. : _M_param(__p)
  4617. { }
  4618. /**
  4619. * @brief Resets the distribution state.
  4620. */
  4621. void
  4622. reset()
  4623. { }
  4624. /**
  4625. * @brief Returns the probabilities of the distribution.
  4626. */
  4627. std::vector<double>
  4628. probabilities() const
  4629. {
  4630. return _M_param._M_prob.empty()
  4631. ? std::vector<double>(1, 1.0) : _M_param._M_prob;
  4632. }
  4633. /**
  4634. * @brief Returns the parameter set of the distribution.
  4635. */
  4636. param_type
  4637. param() const
  4638. { return _M_param; }
  4639. /**
  4640. * @brief Sets the parameter set of the distribution.
  4641. * @param __param The new parameter set of the distribution.
  4642. */
  4643. void
  4644. param(const param_type& __param)
  4645. { _M_param = __param; }
  4646. /**
  4647. * @brief Returns the greatest lower bound value of the distribution.
  4648. */
  4649. result_type
  4650. min() const
  4651. { return result_type(0); }
  4652. /**
  4653. * @brief Returns the least upper bound value of the distribution.
  4654. */
  4655. result_type
  4656. max() const
  4657. {
  4658. return _M_param._M_prob.empty()
  4659. ? result_type(0) : result_type(_M_param._M_prob.size() - 1);
  4660. }
  4661. /**
  4662. * @brief Generating functions.
  4663. */
  4664. template<typename _UniformRandomNumberGenerator>
  4665. result_type
  4666. operator()(_UniformRandomNumberGenerator& __urng)
  4667. { return this->operator()(__urng, _M_param); }
  4668. template<typename _UniformRandomNumberGenerator>
  4669. result_type
  4670. operator()(_UniformRandomNumberGenerator& __urng,
  4671. const param_type& __p);
  4672. template<typename _ForwardIterator,
  4673. typename _UniformRandomNumberGenerator>
  4674. void
  4675. __generate(_ForwardIterator __f, _ForwardIterator __t,
  4676. _UniformRandomNumberGenerator& __urng)
  4677. { this->__generate(__f, __t, __urng, _M_param); }
  4678. template<typename _ForwardIterator,
  4679. typename _UniformRandomNumberGenerator>
  4680. void
  4681. __generate(_ForwardIterator __f, _ForwardIterator __t,
  4682. _UniformRandomNumberGenerator& __urng,
  4683. const param_type& __p)
  4684. { this->__generate_impl(__f, __t, __urng, __p); }
  4685. template<typename _UniformRandomNumberGenerator>
  4686. void
  4687. __generate(result_type* __f, result_type* __t,
  4688. _UniformRandomNumberGenerator& __urng,
  4689. const param_type& __p)
  4690. { this->__generate_impl(__f, __t, __urng, __p); }
  4691. /**
  4692. * @brief Return true if two discrete distributions have the same
  4693. * parameters.
  4694. */
  4695. friend bool
  4696. operator==(const discrete_distribution& __d1,
  4697. const discrete_distribution& __d2)
  4698. { return __d1._M_param == __d2._M_param; }
  4699. /**
  4700. * @brief Inserts a %discrete_distribution random number distribution
  4701. * @p __x into the output stream @p __os.
  4702. *
  4703. * @param __os An output stream.
  4704. * @param __x A %discrete_distribution random number distribution.
  4705. *
  4706. * @returns The output stream with the state of @p __x inserted or in
  4707. * an error state.
  4708. */
  4709. template<typename _IntType1, typename _CharT, typename _Traits>
  4710. friend std::basic_ostream<_CharT, _Traits>&
  4711. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  4712. const std::discrete_distribution<_IntType1>& __x);
  4713. /**
  4714. * @brief Extracts a %discrete_distribution random number distribution
  4715. * @p __x from the input stream @p __is.
  4716. *
  4717. * @param __is An input stream.
  4718. * @param __x A %discrete_distribution random number
  4719. * generator engine.
  4720. *
  4721. * @returns The input stream with @p __x extracted or in an error
  4722. * state.
  4723. */
  4724. template<typename _IntType1, typename _CharT, typename _Traits>
  4725. friend std::basic_istream<_CharT, _Traits>&
  4726. operator>>(std::basic_istream<_CharT, _Traits>& __is,
  4727. std::discrete_distribution<_IntType1>& __x);
  4728. private:
  4729. template<typename _ForwardIterator,
  4730. typename _UniformRandomNumberGenerator>
  4731. void
  4732. __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
  4733. _UniformRandomNumberGenerator& __urng,
  4734. const param_type& __p);
  4735. param_type _M_param;
  4736. };
  4737. /**
  4738. * @brief Return true if two discrete distributions have different
  4739. * parameters.
  4740. */
  4741. template<typename _IntType>
  4742. inline bool
  4743. operator!=(const std::discrete_distribution<_IntType>& __d1,
  4744. const std::discrete_distribution<_IntType>& __d2)
  4745. { return !(__d1 == __d2); }
  4746. /**
  4747. * @brief A piecewise_constant_distribution random number distribution.
  4748. *
  4749. * The formula for the piecewise constant probability mass function is
  4750. *
  4751. */
  4752. template<typename _RealType = double>
  4753. class piecewise_constant_distribution
  4754. {
  4755. static_assert(std::is_floating_point<_RealType>::value,
  4756. "result_type must be a floating point type");
  4757. public:
  4758. /** The type of the range of the distribution. */
  4759. typedef _RealType result_type;
  4760. /** Parameter type. */
  4761. struct param_type
  4762. {
  4763. typedef piecewise_constant_distribution<_RealType> distribution_type;
  4764. friend class piecewise_constant_distribution<_RealType>;
  4765. param_type()
  4766. : _M_int(), _M_den(), _M_cp()
  4767. { }
  4768. template<typename _InputIteratorB, typename _InputIteratorW>
  4769. param_type(_InputIteratorB __bfirst,
  4770. _InputIteratorB __bend,
  4771. _InputIteratorW __wbegin);
  4772. template<typename _Func>
  4773. param_type(initializer_list<_RealType> __bi, _Func __fw);
  4774. template<typename _Func>
  4775. param_type(size_t __nw, _RealType __xmin, _RealType __xmax,
  4776. _Func __fw);
  4777. // See: http://cpp-next.com/archive/2010/10/implicit-move-must-go/
  4778. param_type(const param_type&) = default;
  4779. param_type& operator=(const param_type&) = default;
  4780. std::vector<_RealType>
  4781. intervals() const
  4782. {
  4783. if (_M_int.empty())
  4784. {
  4785. std::vector<_RealType> __tmp(2);
  4786. __tmp[1] = _RealType(1);
  4787. return __tmp;
  4788. }
  4789. else
  4790. return _M_int;
  4791. }
  4792. std::vector<double>
  4793. densities() const
  4794. { return _M_den.empty() ? std::vector<double>(1, 1.0) : _M_den; }
  4795. friend bool
  4796. operator==(const param_type& __p1, const param_type& __p2)
  4797. { return __p1._M_int == __p2._M_int && __p1._M_den == __p2._M_den; }
  4798. friend bool
  4799. operator!=(const param_type& __p1, const param_type& __p2)
  4800. { return !(__p1 == __p2); }
  4801. private:
  4802. void
  4803. _M_initialize();
  4804. std::vector<_RealType> _M_int;
  4805. std::vector<double> _M_den;
  4806. std::vector<double> _M_cp;
  4807. };
  4808. piecewise_constant_distribution()
  4809. : _M_param()
  4810. { }
  4811. template<typename _InputIteratorB, typename _InputIteratorW>
  4812. piecewise_constant_distribution(_InputIteratorB __bfirst,
  4813. _InputIteratorB __bend,
  4814. _InputIteratorW __wbegin)
  4815. : _M_param(__bfirst, __bend, __wbegin)
  4816. { }
  4817. template<typename _Func>
  4818. piecewise_constant_distribution(initializer_list<_RealType> __bl,
  4819. _Func __fw)
  4820. : _M_param(__bl, __fw)
  4821. { }
  4822. template<typename _Func>
  4823. piecewise_constant_distribution(size_t __nw,
  4824. _RealType __xmin, _RealType __xmax,
  4825. _Func __fw)
  4826. : _M_param(__nw, __xmin, __xmax, __fw)
  4827. { }
  4828. explicit
  4829. piecewise_constant_distribution(const param_type& __p)
  4830. : _M_param(__p)
  4831. { }
  4832. /**
  4833. * @brief Resets the distribution state.
  4834. */
  4835. void
  4836. reset()
  4837. { }
  4838. /**
  4839. * @brief Returns a vector of the intervals.
  4840. */
  4841. std::vector<_RealType>
  4842. intervals() const
  4843. {
  4844. if (_M_param._M_int.empty())
  4845. {
  4846. std::vector<_RealType> __tmp(2);
  4847. __tmp[1] = _RealType(1);
  4848. return __tmp;
  4849. }
  4850. else
  4851. return _M_param._M_int;
  4852. }
  4853. /**
  4854. * @brief Returns a vector of the probability densities.
  4855. */
  4856. std::vector<double>
  4857. densities() const
  4858. {
  4859. return _M_param._M_den.empty()
  4860. ? std::vector<double>(1, 1.0) : _M_param._M_den;
  4861. }
  4862. /**
  4863. * @brief Returns the parameter set of the distribution.
  4864. */
  4865. param_type
  4866. param() const
  4867. { return _M_param; }
  4868. /**
  4869. * @brief Sets the parameter set of the distribution.
  4870. * @param __param The new parameter set of the distribution.
  4871. */
  4872. void
  4873. param(const param_type& __param)
  4874. { _M_param = __param; }
  4875. /**
  4876. * @brief Returns the greatest lower bound value of the distribution.
  4877. */
  4878. result_type
  4879. min() const
  4880. {
  4881. return _M_param._M_int.empty()
  4882. ? result_type(0) : _M_param._M_int.front();
  4883. }
  4884. /**
  4885. * @brief Returns the least upper bound value of the distribution.
  4886. */
  4887. result_type
  4888. max() const
  4889. {
  4890. return _M_param._M_int.empty()
  4891. ? result_type(1) : _M_param._M_int.back();
  4892. }
  4893. /**
  4894. * @brief Generating functions.
  4895. */
  4896. template<typename _UniformRandomNumberGenerator>
  4897. result_type
  4898. operator()(_UniformRandomNumberGenerator& __urng)
  4899. { return this->operator()(__urng, _M_param); }
  4900. template<typename _UniformRandomNumberGenerator>
  4901. result_type
  4902. operator()(_UniformRandomNumberGenerator& __urng,
  4903. const param_type& __p);
  4904. template<typename _ForwardIterator,
  4905. typename _UniformRandomNumberGenerator>
  4906. void
  4907. __generate(_ForwardIterator __f, _ForwardIterator __t,
  4908. _UniformRandomNumberGenerator& __urng)
  4909. { this->__generate(__f, __t, __urng, _M_param); }
  4910. template<typename _ForwardIterator,
  4911. typename _UniformRandomNumberGenerator>
  4912. void
  4913. __generate(_ForwardIterator __f, _ForwardIterator __t,
  4914. _UniformRandomNumberGenerator& __urng,
  4915. const param_type& __p)
  4916. { this->__generate_impl(__f, __t, __urng, __p); }
  4917. template<typename _UniformRandomNumberGenerator>
  4918. void
  4919. __generate(result_type* __f, result_type* __t,
  4920. _UniformRandomNumberGenerator& __urng,
  4921. const param_type& __p)
  4922. { this->__generate_impl(__f, __t, __urng, __p); }
  4923. /**
  4924. * @brief Return true if two piecewise constant distributions have the
  4925. * same parameters.
  4926. */
  4927. friend bool
  4928. operator==(const piecewise_constant_distribution& __d1,
  4929. const piecewise_constant_distribution& __d2)
  4930. { return __d1._M_param == __d2._M_param; }
  4931. /**
  4932. * @brief Inserts a %piecewise_constant_distribution random
  4933. * number distribution @p __x into the output stream @p __os.
  4934. *
  4935. * @param __os An output stream.
  4936. * @param __x A %piecewise_constant_distribution random number
  4937. * distribution.
  4938. *
  4939. * @returns The output stream with the state of @p __x inserted or in
  4940. * an error state.
  4941. */
  4942. template<typename _RealType1, typename _CharT, typename _Traits>
  4943. friend std::basic_ostream<_CharT, _Traits>&
  4944. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  4945. const std::piecewise_constant_distribution<_RealType1>& __x);
  4946. /**
  4947. * @brief Extracts a %piecewise_constant_distribution random
  4948. * number distribution @p __x from the input stream @p __is.
  4949. *
  4950. * @param __is An input stream.
  4951. * @param __x A %piecewise_constant_distribution random number
  4952. * generator engine.
  4953. *
  4954. * @returns The input stream with @p __x extracted or in an error
  4955. * state.
  4956. */
  4957. template<typename _RealType1, typename _CharT, typename _Traits>
  4958. friend std::basic_istream<_CharT, _Traits>&
  4959. operator>>(std::basic_istream<_CharT, _Traits>& __is,
  4960. std::piecewise_constant_distribution<_RealType1>& __x);
  4961. private:
  4962. template<typename _ForwardIterator,
  4963. typename _UniformRandomNumberGenerator>
  4964. void
  4965. __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
  4966. _UniformRandomNumberGenerator& __urng,
  4967. const param_type& __p);
  4968. param_type _M_param;
  4969. };
  4970. /**
  4971. * @brief Return true if two piecewise constant distributions have
  4972. * different parameters.
  4973. */
  4974. template<typename _RealType>
  4975. inline bool
  4976. operator!=(const std::piecewise_constant_distribution<_RealType>& __d1,
  4977. const std::piecewise_constant_distribution<_RealType>& __d2)
  4978. { return !(__d1 == __d2); }
  4979. /**
  4980. * @brief A piecewise_linear_distribution random number distribution.
  4981. *
  4982. * The formula for the piecewise linear probability mass function is
  4983. *
  4984. */
  4985. template<typename _RealType = double>
  4986. class piecewise_linear_distribution
  4987. {
  4988. static_assert(std::is_floating_point<_RealType>::value,
  4989. "result_type must be a floating point type");
  4990. public:
  4991. /** The type of the range of the distribution. */
  4992. typedef _RealType result_type;
  4993. /** Parameter type. */
  4994. struct param_type
  4995. {
  4996. typedef piecewise_linear_distribution<_RealType> distribution_type;
  4997. friend class piecewise_linear_distribution<_RealType>;
  4998. param_type()
  4999. : _M_int(), _M_den(), _M_cp(), _M_m()
  5000. { }
  5001. template<typename _InputIteratorB, typename _InputIteratorW>
  5002. param_type(_InputIteratorB __bfirst,
  5003. _InputIteratorB __bend,
  5004. _InputIteratorW __wbegin);
  5005. template<typename _Func>
  5006. param_type(initializer_list<_RealType> __bl, _Func __fw);
  5007. template<typename _Func>
  5008. param_type(size_t __nw, _RealType __xmin, _RealType __xmax,
  5009. _Func __fw);
  5010. // See: http://cpp-next.com/archive/2010/10/implicit-move-must-go/
  5011. param_type(const param_type&) = default;
  5012. param_type& operator=(const param_type&) = default;
  5013. std::vector<_RealType>
  5014. intervals() const
  5015. {
  5016. if (_M_int.empty())
  5017. {
  5018. std::vector<_RealType> __tmp(2);
  5019. __tmp[1] = _RealType(1);
  5020. return __tmp;
  5021. }
  5022. else
  5023. return _M_int;
  5024. }
  5025. std::vector<double>
  5026. densities() const
  5027. { return _M_den.empty() ? std::vector<double>(2, 1.0) : _M_den; }
  5028. friend bool
  5029. operator==(const param_type& __p1, const param_type& __p2)
  5030. { return __p1._M_int == __p2._M_int && __p1._M_den == __p2._M_den; }
  5031. friend bool
  5032. operator!=(const param_type& __p1, const param_type& __p2)
  5033. { return !(__p1 == __p2); }
  5034. private:
  5035. void
  5036. _M_initialize();
  5037. std::vector<_RealType> _M_int;
  5038. std::vector<double> _M_den;
  5039. std::vector<double> _M_cp;
  5040. std::vector<double> _M_m;
  5041. };
  5042. piecewise_linear_distribution()
  5043. : _M_param()
  5044. { }
  5045. template<typename _InputIteratorB, typename _InputIteratorW>
  5046. piecewise_linear_distribution(_InputIteratorB __bfirst,
  5047. _InputIteratorB __bend,
  5048. _InputIteratorW __wbegin)
  5049. : _M_param(__bfirst, __bend, __wbegin)
  5050. { }
  5051. template<typename _Func>
  5052. piecewise_linear_distribution(initializer_list<_RealType> __bl,
  5053. _Func __fw)
  5054. : _M_param(__bl, __fw)
  5055. { }
  5056. template<typename _Func>
  5057. piecewise_linear_distribution(size_t __nw,
  5058. _RealType __xmin, _RealType __xmax,
  5059. _Func __fw)
  5060. : _M_param(__nw, __xmin, __xmax, __fw)
  5061. { }
  5062. explicit
  5063. piecewise_linear_distribution(const param_type& __p)
  5064. : _M_param(__p)
  5065. { }
  5066. /**
  5067. * Resets the distribution state.
  5068. */
  5069. void
  5070. reset()
  5071. { }
  5072. /**
  5073. * @brief Return the intervals of the distribution.
  5074. */
  5075. std::vector<_RealType>
  5076. intervals() const
  5077. {
  5078. if (_M_param._M_int.empty())
  5079. {
  5080. std::vector<_RealType> __tmp(2);
  5081. __tmp[1] = _RealType(1);
  5082. return __tmp;
  5083. }
  5084. else
  5085. return _M_param._M_int;
  5086. }
  5087. /**
  5088. * @brief Return a vector of the probability densities of the
  5089. * distribution.
  5090. */
  5091. std::vector<double>
  5092. densities() const
  5093. {
  5094. return _M_param._M_den.empty()
  5095. ? std::vector<double>(2, 1.0) : _M_param._M_den;
  5096. }
  5097. /**
  5098. * @brief Returns the parameter set of the distribution.
  5099. */
  5100. param_type
  5101. param() const
  5102. { return _M_param; }
  5103. /**
  5104. * @brief Sets the parameter set of the distribution.
  5105. * @param __param The new parameter set of the distribution.
  5106. */
  5107. void
  5108. param(const param_type& __param)
  5109. { _M_param = __param; }
  5110. /**
  5111. * @brief Returns the greatest lower bound value of the distribution.
  5112. */
  5113. result_type
  5114. min() const
  5115. {
  5116. return _M_param._M_int.empty()
  5117. ? result_type(0) : _M_param._M_int.front();
  5118. }
  5119. /**
  5120. * @brief Returns the least upper bound value of the distribution.
  5121. */
  5122. result_type
  5123. max() const
  5124. {
  5125. return _M_param._M_int.empty()
  5126. ? result_type(1) : _M_param._M_int.back();
  5127. }
  5128. /**
  5129. * @brief Generating functions.
  5130. */
  5131. template<typename _UniformRandomNumberGenerator>
  5132. result_type
  5133. operator()(_UniformRandomNumberGenerator& __urng)
  5134. { return this->operator()(__urng, _M_param); }
  5135. template<typename _UniformRandomNumberGenerator>
  5136. result_type
  5137. operator()(_UniformRandomNumberGenerator& __urng,
  5138. const param_type& __p);
  5139. template<typename _ForwardIterator,
  5140. typename _UniformRandomNumberGenerator>
  5141. void
  5142. __generate(_ForwardIterator __f, _ForwardIterator __t,
  5143. _UniformRandomNumberGenerator& __urng)
  5144. { this->__generate(__f, __t, __urng, _M_param); }
  5145. template<typename _ForwardIterator,
  5146. typename _UniformRandomNumberGenerator>
  5147. void
  5148. __generate(_ForwardIterator __f, _ForwardIterator __t,
  5149. _UniformRandomNumberGenerator& __urng,
  5150. const param_type& __p)
  5151. { this->__generate_impl(__f, __t, __urng, __p); }
  5152. template<typename _UniformRandomNumberGenerator>
  5153. void
  5154. __generate(result_type* __f, result_type* __t,
  5155. _UniformRandomNumberGenerator& __urng,
  5156. const param_type& __p)
  5157. { this->__generate_impl(__f, __t, __urng, __p); }
  5158. /**
  5159. * @brief Return true if two piecewise linear distributions have the
  5160. * same parameters.
  5161. */
  5162. friend bool
  5163. operator==(const piecewise_linear_distribution& __d1,
  5164. const piecewise_linear_distribution& __d2)
  5165. { return __d1._M_param == __d2._M_param; }
  5166. /**
  5167. * @brief Inserts a %piecewise_linear_distribution random number
  5168. * distribution @p __x into the output stream @p __os.
  5169. *
  5170. * @param __os An output stream.
  5171. * @param __x A %piecewise_linear_distribution random number
  5172. * distribution.
  5173. *
  5174. * @returns The output stream with the state of @p __x inserted or in
  5175. * an error state.
  5176. */
  5177. template<typename _RealType1, typename _CharT, typename _Traits>
  5178. friend std::basic_ostream<_CharT, _Traits>&
  5179. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  5180. const std::piecewise_linear_distribution<_RealType1>& __x);
  5181. /**
  5182. * @brief Extracts a %piecewise_linear_distribution random number
  5183. * distribution @p __x from the input stream @p __is.
  5184. *
  5185. * @param __is An input stream.
  5186. * @param __x A %piecewise_linear_distribution random number
  5187. * generator engine.
  5188. *
  5189. * @returns The input stream with @p __x extracted or in an error
  5190. * state.
  5191. */
  5192. template<typename _RealType1, typename _CharT, typename _Traits>
  5193. friend std::basic_istream<_CharT, _Traits>&
  5194. operator>>(std::basic_istream<_CharT, _Traits>& __is,
  5195. std::piecewise_linear_distribution<_RealType1>& __x);
  5196. private:
  5197. template<typename _ForwardIterator,
  5198. typename _UniformRandomNumberGenerator>
  5199. void
  5200. __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
  5201. _UniformRandomNumberGenerator& __urng,
  5202. const param_type& __p);
  5203. param_type _M_param;
  5204. };
  5205. /**
  5206. * @brief Return true if two piecewise linear distributions have
  5207. * different parameters.
  5208. */
  5209. template<typename _RealType>
  5210. inline bool
  5211. operator!=(const std::piecewise_linear_distribution<_RealType>& __d1,
  5212. const std::piecewise_linear_distribution<_RealType>& __d2)
  5213. { return !(__d1 == __d2); }
  5214. /// @} group random_distributions_poisson
  5215. /// @} *group random_distributions
  5216. /**
  5217. * @addtogroup random_utilities Random Number Utilities
  5218. * @ingroup random
  5219. * @{
  5220. */
  5221. /**
  5222. * @brief The seed_seq class generates sequences of seeds for random
  5223. * number generators.
  5224. */
  5225. class seed_seq
  5226. {
  5227. public:
  5228. /** The type of the seed vales. */
  5229. typedef uint_least32_t result_type;
  5230. /** Default constructor. */
  5231. seed_seq() noexcept
  5232. : _M_v()
  5233. { }
  5234. template<typename _IntType>
  5235. seed_seq(std::initializer_list<_IntType> __il);
  5236. template<typename _InputIterator>
  5237. seed_seq(_InputIterator __begin, _InputIterator __end);
  5238. // generating functions
  5239. template<typename _RandomAccessIterator>
  5240. void
  5241. generate(_RandomAccessIterator __begin, _RandomAccessIterator __end);
  5242. // property functions
  5243. size_t size() const noexcept
  5244. { return _M_v.size(); }
  5245. template<typename _OutputIterator>
  5246. void
  5247. param(_OutputIterator __dest) const
  5248. { std::copy(_M_v.begin(), _M_v.end(), __dest); }
  5249. // no copy functions
  5250. seed_seq(const seed_seq&) = delete;
  5251. seed_seq& operator=(const seed_seq&) = delete;
  5252. private:
  5253. std::vector<result_type> _M_v;
  5254. };
  5255. /// @} group random_utilities
  5256. /// @} group random
  5257. _GLIBCXX_END_NAMESPACE_VERSION
  5258. } // namespace std
  5259. #endif