random.h 171 KB

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