我需要使用GMP库生成浮点随机数。我只需要在0和1之间均匀分布的任意精度浮点数。我试过这个:
mpf_t a;
mpf_set_default_prec(1000000);
mpf_init(a);
mpf_random2(a, 1000000, 0);
gmp_printf("%.250000Ff\n\n", a);
打印出来
0.000000000000000222044604925031308084726333618164062499999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999082607122263075860891192663806466793534363298000186067539455549216527797629108224022249281397305624578226176472470467635302841264178043382752860171325188111220170721342790883551213485202433891088306798488160049318965412314358344223872531457409245001213525152998316172646701234261639355416216502449020499816872309526262160071145300404893836434815095991985649104156676377890559780596615540948803239588636312719066886186643144714770146842863996353062030456183477450464729028542051823459809366093722933818224742729708744971727119726771572150589908234689715356879984506564961617769615631650353852405635310454446555844186494008143588247337607812518643358634630734050235144686626424275460590699808659681320943033494516454976050935759559505157151167724602518939809905618291417882199962975866639893180176423853835707218782544732181025313514757689486608129975414378449514057361368337625191040336283029084307235173320798998480489511543283739339038497024137794068335122493563642284921971609877669689133609069101879131764075791633781304452881527485082493118932271190600577999063270404738526376877770718281401626220871674077197553482715717176064977969178487879513268230386087966448456506259918570215673749511202007075762796095917576821605697910153486700354718645546298177102645287727167562067214788707371707428568031041471576870982151609782504377426695268116084565508507619842980378792788273732833181979486379335055396751739628345531976333278020077855419095547871838392600166109237557986216863024654569226112659646793740613059837686012790630037294611293872359593385587183076988563371304412876381689589560518128669931702647458166122575912101962134625348186081712337119193248037660742995917112659359392212010149505343360255512848497288597135866576926294693900870252215397880998534311161530734776090973888008254670955639101157446190989878993843783737071845300619163774617604521894571463611584230387501626668294700229969848842572786818287009431485917881205156545937429021621401469398556072980055470921100188219798086207567345714841451492298391414592984336309183665299605000471290135064634748837486450448173752039014271124348746295119221537304404913884636594677509370209111856450000323164809096181572180446788674355505157261714680348517496366845865465092468747802993951637101983581192572463282557334129974368380091359019053210479766412987002489886403079149204343237896408999944768610477671213018548003638442432761639981459547709156555296623860149944943755650717680205031737937522268690845543331289515962107783962848891316470581633944593053292694243941680803190643560186682744008395309289615343588228395485338447268613952316038520372196587620878592757728261041079289549817982465876502636501976352868244115605577311210260423199241790008822383486617750169975973868162199500071487215190931022766776020197168613334407579308662492219465255842863461358582293869030949627476503408567347711270994946920219523038696973558701923348800997104015095114754520207772826934836275036665730007837932513116743788962045969728232762335509383703399511062412254449168929930812974395835117480867976083007893365356512996030022518770959410492759151576865555677090723150886015550633221523906849084952685166030873670827412878611848746950383522959997608543584558485981964247159935822594023159753597104453746030951293823011177940793961013320782626973311460295809526964280650999613275023004812619224265855041679799508168992809773366606234106475518581282566360088905562270187703853228646784234267771057696025315255106764640316557406757807166154353095392050378782575808853179782368204891366856301973692683698611440552675785932676166029469863735602916818019421260953014354103777078581406983512988631173403399013702078646210493580871073390073795933659511509315365671653872104960680622209343165975296513692933928643561095232095846476620871053159716400124750936580577688760818686749204794389580273152943320345335650384893182451897977333658026489984139147717931833569819146594824518958559988312451611254190361249618415676052913987488244932151287740887961642535723981898941121349589711566863305761340979440403057467111197214599951918271111053290133127662261380587162919732914053326752246195030290037738829438273795266592920388001316624712841969851489678418741570003367742167354712501084887381289444043006249227573974109740882932726472363837574657069822617403085842296168020733809594457415864724467630076174429382406732637094174248707838029844192638371771553817399251361664466209117311918781727419771539026022039957145318962530605937341468400651940099748848946955430865463562939384621901243111445086266188966242676128041253301441736688872662970780567489676261953375936382474481230859460595791147118551432027652831624043492214950431348273530074565686992195467800150478068018015160203641357801761320028712119629869294404674827756949291638351174812109160531654114098910784631534647382119594787916484048977669882163571798215346079196290910360592590464152713091816154538957089203371694997282612357923997317666115915483663664067917303129333251770600898741126380041311166993883642066186668955205840545092920473573443019959793251788373734997606412168000241916196894746748294240979306283241084653693689068836386269325734735804618456487353790971266258042441338077313725613421151699062989141100669042852542638479108983642516043500716725994610190288639971164073894314429533654596713641166512792246354973275119600065556965407783772782267335293771617822311737184750098537668767284295734209403499185003363166005412270348888232629812591511600058363756563683929239931049639271392620434165815743711749481848029734668035778966845546383332706921548226076439465619056170418164943881787906032021135239351034720623369634294903057235791747946082264516684778122961581340929802715187773685715526311240653048787550649531853290856028592709839337026679937127950593887161643342837651299052833983765998964239801873290311241826042633984545337466842418473735154457461978432972928170972047462408269691224791885667785399198006027653160824393373989773139157600620750344921461336712490087886968497334044447208804645376006990534528401039109464003854320948353949131701147747857330981050861214739971689520452532486983160418165235081748219291547031480108843857307542586803773610018201741699863595888067937403413157218207640521013065394374126095413950277467187055217642668383853753962086483975848708558604861295116622889866867134863003702343655569648979239007007397020360812493946134182015388434506325877685028983605320093277398462010476596880056855732417431205219655373177307973260387326760459002068134731664590553975705099290020520559350779508663413019273048373730390604418883795576922158529644570591727607530957070352769593569793430491748972486711975573627053790456153029219085117821865518620975389387452075764476117838784847507252991175478367139656048225520664937144075465291417706736472291458760385851042421922547586568093336456641117894886932538660623569797321327271069088221987456866751441330294841252180506293628296566656978301477676652100604914247196013061606312247642246276974187760634862435391931145079122780093095909833898769376901255220023781125238496138729332436927158095642617096076743028640198436579481845729030072996701252489334655031983689200584428202463298832620642856884260057020442083939440733478510010529568171543579591651399374744502756002532950675203904595738289686342906614716068142014197115398974371649833047066529444376158180069425172680631449399353765245329075251445214071768527938654764774093362812929320161746451446915757529450087536314808137178805393416077013266731871043486524633962400243171830153460345888656107754112346006435792457284025247072584341308851453981688806351022135220510252206091073939058374594387193483150935629034038092124272287212854788038994222757382234635717277327652144685311292218457391905536160813815095721448623399625038050327534134779831271880006649346940841113726360979008881718187198264538461384698799087851375897629512447526126236754964287060420720418215547913253693814867169902585722589847097503442532276867957533566042221320291263651009185117813013954456906441445920378530297034899692513132447741985137519774666198021760728345426042215128933393909352345856783624654402973132746978892622985515555153060429828124510040765549313903923131890185479370204847625444255974456703652596912697685384589042084159577214478975572022142636957469119215193145993971995934916957114402337624463557458710702875207326279913994248481491939876372653883568108327113405298379885108352748637390176786993375419933514523431235879140529805142267924957193521251855459553137612134585072492138367115901000647216401640226482885795247872552339363049339841868275190118716965640285854040960566182082653981065475334417534954710939677987754209248139806463692647976726522102787438679314271175580766335997529961864142320705895496917622829101483591036277963631342048477521066699769087490458737244546011608113662729288143777115644275547262539292746548621374292675698301949067242386179348960018355182522382941260330352013974927457728784618426813022631799486989234624656966699119446013382867119362907573436222300907333408345080943270560034650738983617974929669106335157136002362011210720848180443972539828405167011334600474854010083278910450785237078333942519875241574005149905604026336520419525737162569048347939310825559540106055127811489774067351404361379604911843365895091715750428253103130238835013495543595455699272773958082045515068932366763107409885256200407980726629381171521689778204251436084280910894905467065883124226050236530260966664997586421348892888508620956524380749162383749745233807347632405474882345681836772048359502170875554822558601137674873725466572762695901554803176132491290016606425464655438607233451119966219260924715632523183160442368161901255656849626368020997315076758922779367108314146657915075248988136672545921481844518223400964267612768298440828543290002971519431621241534103128327002276950431942751973241686135758269218474883299458642511091713731938388353841589098681003063171741489368156837832565186681756525101035714937434523561316341364251930754232711428656593373634382991598169944529699464409280348081607797758327193365861137616550335608706930449964496888816506792026287310751956942114729225151670436717924948149114779922621634226063583886929109470735288849540964130089810991192126645363416115620883718685324821941677812912826466529898681384606062049206695753873431124092847829787210609549781226661939322423811442802163329391146904436392854271059209819528572803821638608693282994100175399856561130909751010441880832834536448164718614524511675803411458826276190559101262811992442682308158946408636256058208651389410623776218786978233183620753257126908641846258232231692478736582511900149487250290266696761227316309194224884746093581266347716090674012765170208391970662655954787134689264267987051422757617774677612412381705397673236680259187257234137010092840428520454742480835362044374250625513467298251280993274376426775677359866104990804837568728562573867257936065755941431559149907824435686142039115135877635397312323955059693376956160975533070108258747049190230493553520761901323830711524080576183094799959445999062228215146697055633679384505952511128108132052682706471644697254075180459351491712618310209951847847559697390763982882292818171592575913145960342831916397998415149305488923703311044144525486804739622579239765617285684559329817316496472649480781131996007402406317846696829395248527807688908536641700592661645941303400598365588448566596500589992229443039940214762582457630047693006845981602116973987293911452920888149524787048474656117709147933025533618627461111832772885463682079648861309708870902697696951640886680111218791419318126602060178183051249423468499806325103843402203597756049644819492582633426346955941432247866494945202887178659634287068933189712894011893737564552366423966556991420637191006893201903535700214912021238018496839705223284426760387972617818446217524050911605632741116662003794949315784900857174749493816822942220181682243978714438915180214889060116371630375037597917328174837467084347962542410580902094633541197775078129656142592562225890900161225625303203027052618834824714285670756995075107503986886765586223112401714894141411542165818246601002321068363939235446898820358227253374955246934150926087938999547806057683611508831349601520371391993295136862481223937485471641163463699669280990302446015931689438598197879312715834570724436714650285464796397190938531723841134223419598777435683097095420654935942091468786463102887008710474460680907262827462047321572562193845909092562173159425415279540498448944658468803680308679905954481124767135635244650286832851097768657394431293661948973406272077603177508274094977651521561990823617740676238715609379707168296872858648886083076866410735603350655168321682595174755427530979923888388222545535494853336605996976567842757837665361251182993185659834626145019505315969969668004929526473674196031833400507528378684703996400884212381840497593551766603456274311057554863347197107299584234295682852060249588381613600335055370138059870102607545976568472140434787118846813658827983901314842218107781903739556721492597709437959990499528567059833920520310033962530656177963467997257795942675367975859254898931150484298755118838096739625717906516188125480772620568068338706341934656539123185265432709633726202237543696887233510222103510295294547701964677174409132498788897791101889805365972835609001028256359934731956020879482876725796603081256488003092609691499477807924513328294104043659292375971397140311723230917505736150582250842703821675049923500389155876336543487571854398620325184273979836801176720814455505394933842947658945069243432898248534567334035407823062135155543202397741120959284393661312536389158547422856006033624085617112173531027628625091775550150585719518122777473835934342762944673171750170707559026727511506389049565598525169079113746893335825108194048983424660673463110316349673170199909669927697129992431457300510395353441055390278325530682370984568268630243893149853101195197266842706629473501366569014094367833671628894123194679279225742343719451875037313560471021672334202338348535939604938594144791290507700814220327753445725973338719361634047727379226856526399190602250102939102786293216455491290972079690054631176915596208903418778925707209508564864714813292606652928824495834581592732853212773239348887919765509243466267532980245247232641115701817380695276952215490953959273343236294331278386777649232397302916018044104362412627845200687833230220172349309950788677292654837975657668265941774038705099884244740067792201865546706083351233532933670189693752177449791217613682824958789779360604114483001876458186169688292914803348541149000914713694757161596593723290255410738472694902048730555494446493646236727006456284234210483916685741077206689004300386068064812562290726850879187613163315643889554690698829263640889931792828707523643064435371744112770275907128530481853966562850335548484168797616290791906453022022589297602922584656332485869649566678938830815062710126500493655068571312365012423273946600184196207088636511340878790694220104553427541793543133742968503848921040668885831024104461583165936314106345878580496448593900050693277663378041737256970604632556701713869690189247475824807795397542182522093836320891960232851528122566137204086749346284105932028590297998845499570481430247156031670481386595998750500195944655131738803785964212504663260482860065440889259122763801347107886427861919502406497054287945763394939090046984760463494304318610876706216964082803287016833493618990573112036157533579005395528287208714048058933502847006802955220058649363582148419379810285101039795718634928164092411643135874675941655935778201103580648099061654574353002931821439011200754892078611093849368430960549793210618605980429659194190000292033581775153762170181439760811386737328313335581468543188635824514933339201559826252958915178097518626413158869532013115181454014864361393488834101259111194464206021980286580128918079397996556696672845654255891258641794334039733496151613952605189387999547556728951711851718950029567481845985806626373692702062169209167529614292727676374823904287179447738154629152702692060084383614759619229886071750788626316090327782985727631275401600258678219289602306604903882540695864021571395207465107413521350311437815588728917937403558392469608899629122059350407843266200878075106656612295516654531438550604350716592666343264666513008280400720768912265124147169650354856613429366087518777729053543621609953350480197823487852266222211575980181923909405240828806527671157931466403791999628371269707164521857632591047327294439881056920272821714598583495375966388047762420572508649178528449552976360424403911333529277674105684139729276058844415321606234433433337377094500964200889602921933335013874821100897283909239718364223192921057331496580008016979112445905757450785556254907265764014103175040871618747790315288797466291642916548748700797598738233507128446323736512198161005568492041945840430734379446389278201841330108412033726255994748638264785151152110542134589658714415127706024118785458070236067239386434413779795397598065647469401589737581844204110855824605603372625209440291480211361066634207102583254917972503915399250571420500624538489188756492995610322232406134694187637468266855269042165944535801355237422053862468170705800474345118508077471069020620127033651566177834550737372567667788157879136701543985592037956641904285419845956906603673835408523742708915008695611045221473496663348152690287849495263780501169970703405872869227239303274050337619138680101969657723062735291591994507183632299699921058597180019126682348756823114561745595213174621955432147264961810749636016161582929131247473171597684332996889222839898586110925422092856165893985715391500998805718008437055980849444510824961509066908773528540057450720076727601178368899468945395029006290698036486143969994458530344955256310771521101495201054653019743890777488066802444704058904781959646414305683074225030751321677402836052314569608069039124219826625603212393345763963641695019907596476660626091550997311276301871816776178530666181815393900486722345118397985801198113184058023766385539068249963548235034695915238006472864590189321498022420467137650317481629117143799972817666545617462206701916830554202153123940061729671992568178900363869740444790339374639128587818754634997915762955605167935987920528832413924919164257824029573710572574854179509776260284166267438935235845425239632825508159915372707786681993776741285170630668584188873002152028268916571954938421688024540114296829426672194069714240533425921119158281335684776744655886338925992514782885904286628393884195145793652978945011731721621777391811669004465879976533268120929630514389083845751717476941016783928147539543415574714916014692604148612274507428728994178678660788905915846214941865416202111407565788909571238434985713822563160114711840495806212081667544442247165493803924926320214284022739447463711600333762117165011936591998770278109884201315004139054017128513214314234429698551891537297849708602244285172267566214643484677212220089916316465729657098920790617721596036590275174842202154877081449697107488369252100406938194838865143633774037268795090098942558460904143636798097807742909079402671455596223555693298916738731829554067683666618065071206620636189846327241661452879464495504324565865367443530562930222997445485080444098693566339166112447486794541472762739622729834219667933054127247099841268071410157137603954105413547943618975326980335350823620087074035485382223134498664179005675721694312635308318874565455775722448286195115107639456644734907589228481391170938433397254558697068412149887075207527875044674676657027478356412872764693891296534864789017612030323698709710204386317037804615286044079
这真的不是一个随机数!我只是不了解那些巨大的9s系列以及一开始就有大量的0!也许我使用了错误的功能?
答案 0 :(得分:2)
来自docs:
- 功能:mpf_class gmp_randclass :: get_f(mp_bitcnt_t prec)
生成0 <= f <0的范围内的随机浮点数f。 1. f将是精确到位的精度,或者如果没有给出精度到目的地的精度。例如,
gmp_randclass r;
mpf_class f (0, 512); // 512 bits precision
f = r.get_f(); // random number, 512 bits