random.h 179 KB

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