random.h 174 KB

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