random.h 174 KB

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