Random numbers: Difference between revisions

2,151 bytes removed ,  1 month ago
→‎{{header|BASIC}}: Added ANSI BASIC.
(→‎{{header|BASIC}}: Added ANSI BASIC.)
(48 intermediate revisions by 24 users not shown)
Line 8:
Generate a collection filled with   '''1000'''   normally distributed random (or pseudo-random) numbers
with a mean of   '''1.0'''   and a   [[wp:Standard_deviation|standard deviation]]   of   '''0.5'''
 
 
Many libraries only generate uniformly distributed random numbers. If so, you may use [[wp:Normal_distribution#Generating_values_from_normal_distribution|one of these algorithms]].
 
 
;Related task:
Line 18 ⟶ 16:
 
=={{header|Ada}}==
<langsyntaxhighlight lang="ada">with Ada.Numerics; use Ada.Numerics;
with Ada.Numerics.Float_Random; use Ada.Numerics.Float_Random;
with Ada.Numerics.Elementary_Functions; use Ada.Numerics.Elementary_Functions;
Line 40 ⟶ 38:
Distribution (I) := Normal_Distribution (Seed);
end loop;
end Normal_Random;</langsyntaxhighlight>
 
=={{header|ALGOL 68}}==
Line 49 ⟶ 47:
{{works with|ALGOL 68G|Any - tested with release [http://sourceforge.net/projects/algol68/files/algol68g/algol68g-1.18.0/algol68g-1.18.0-9h.tiny.el5.centos.fc11.i386.rpm/download 1.18.0-9h.tiny]}}
{{wont work with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release [http://sourceforge.net/projects/algol68/files/algol68toc/algol68toc-1.8.8d/algol68toc-1.8-8d.fc9.i386.rpm/download 1.8-8d] - due to extensive use of FORMATted transput}}
<langsyntaxhighlight lang="algol68">PROC random normal = REAL: # normal distribution, centered on 0, std dev 1 #
(
sqrt(-2*log(random)) * cos(2*pi*random)
Line 61 ⟶ 59:
INT limit=10;
printf(($"("n(limit-1)(-d.6d",")-d.5d" ... )"$, rands[:limit]))
)</langsyntaxhighlight>
{{out}}
<pre>
Line 69 ⟶ 67:
=={{header|Arturo}}==
 
<langsyntaxhighlight lang="rebol">rnd: function []-> (random 0 10000)//10000
 
rands: map 1..1000 'x [
Line 75 ⟶ 73:
]
 
print rands</langsyntaxhighlight>
 
{{out}}
 
<pre>0.6219537961087694 1.279396486161406 1.619019280815647 2.119538294228789 -0.1598383851981044 2.67797211803156 0.9304703037226587 1.629254364659528 -0.4171704717398712 0.9082342931486092 -0.5929704390625219 2.117000897984871 -0.1981633787460266 0.01132471973856153 2.102359263212924 0.2408823232884222 2.046195035792376 0.6374831627030295 0.000839808324124558 1.117061838266626 0.7413355299469649 0.4485598815755762 2.999434800016997 2.560580541932842 1.703197984879731 2.889159248353575 -1.800157205708138 1.756810020187321 0.7136708180852145 0.5929151678321705 0.332519993787973 2.660212054362758 0.5835660585480075 0.8527946892567934 1.640573993747053 0.09471843345263908 1.051402997891346 1.116149156137905 -0.7400139019343499 1.782572831979232 2.531779039786426 0.5240268064639871 0.07099232630526586 -0.854892656700071 1.54381929430469 -0.4416899008614745 0.4274356035015117 0.7350027625573482 2.153583935076981 1.461215281535983 -1.041723064151266 2.338060763553139 -0.1492967916030414 0.3799517724040202 0.4577924541353815 0.673317567666373 -2.27731583876462 1.28480355806061 -0.6925811023772748 -0.2642224122781984 0.6590513830891744 2.55537133425143 1.67933335469247 0.8659013395355968 -1.211026941441126 0.9524579534222226 -0.1931750631835656 -0.5119479869237693 0.1814749003063878 3.03139579963414 ...</pre>
<pre>0.6219537961087694 1.279396486161406 1.619019280815647 2.119538294228789 -0.1598383851981044 2.67797211803156 0.9304703037226587 1.629254364659528 -0.4171704717398712 0.9082342931486092 -0.5929704390625219 2.117000897984871 -0.1981633787460266 0.01132471973856153 2.102359263212924 0.2408823232884222 2.046195035792376 0.6374831627030295 0.000839808324124558 1.117061838266626 0.7413355299469649 0.4485598815755762 2.999434800016997 2.560580541932842 1.703197984879731 2.889159248353575 -1.800157205708138 1.756810020187321 0.7136708180852145 0.5929151678321705 0.332519993787973 2.660212054362758 0.5835660585480075 0.8527946892567934 1.640573993747053 0.09471843345263908 1.051402997891346 1.116149156137905 -0.7400139019343499 1.782572831979232 2.531779039786426 0.5240268064639871 0.07099232630526586 -0.854892656700071 1.54381929430469 -0.4416899008614745 0.4274356035015117 0.7350027625573482 2.153583935076981 1.461215281535983 -1.041723064151266 2.338060763553139 -0.1492967916030414 0.3799517724040202 0.4577924541353815 0.673317567666373 -2.27731583876462 1.28480355806061 -0.6925811023772748 -0.2642224122781984 0.6590513830891744 2.55537133425143 1.67933335469247 0.8659013395355968 -1.211026941441126 0.9524579534222226 -0.1931750631835656 -0.5119479869237693 0.1814749003063878 3.03139579963414 0.8041947090501279 -0.01865777860269424 -1.345117694187226 1.172047503199659 -0.005768598737427855 0.4430753828101858 -0.04896736045307204 0.4895169217506786 1.048229080758918 2.149038787105586 2.027034431271556 2.432282801435372 2.53693570310459 -0.5968142206719835 1.445180766446883 1.993843935917086 0.3246477684427571 0.2214120156342799 2.123978740745019 0.7660225685637785 -0.4679548030089944 1.892651338960027 -1.082672493660939 1.388650546130418 0.8586892232800707 0.1688680513907505 0.2031720169757011 1.0622153495636 2.237489272627846 0.489619531018731 2.067023562273573 3.232122397451699 -0.03837283616929521 -0.8782247037247171 2.155998134902104 0.5475989225547847 1.468927649628817 1.27719993843459 1.237163843541324 1.73794547561703 0.8804315237620751 0.02508158115251735 1.65857224365674 1.078989935949705 0.7164357441628163 1.71375577930128 0.6629629974248089 1.584782749537475 0.5168549101430115 2.435706381815515 2.458518058181382 0.4983726815666553 1.662610001403874 0.5100242881482009 0.424252537753539 2.874829969968073 2.739428722137513 1.568599755919953 1.17450447863347 0.2147990014354625 0.94960001217684 1.530842927153068 1.033241575011026 0.7136369721788152 -1.752151570685991 -0.1825011364213669 0.7632351752546898 -0.705217801387372 1.146249503264144 0.5024360662432521 -0.05514204232437669 2.751909795371251 1.404999848001164 0.682474910779916 2.333698852277054 -0.3385164481257155 0.7477589125734123 0.7499986390595292 1.357508529051189 0.5882691367699802 0.6512207531472127 0.7682407661401032 1.256155994922453 0.5223880933125757 4.397210245005706 0.1876049987895129 -0.4165321555980093 0.1177916886895712 1.418593319932079 0.6245130605510126 0.03422663901416945 -0.126508344317811 0.6975972877438945 1.322572198342049 1.386458671539238 2.18031821792089 0.718913947303726 -0.4766346702601931 3.536427477250057 1.346365414270038 1.846533142607893 1.569841840872839 1.802939152281028 0.8756670850485474 0.951865353598341 0.958791363972665 1.157743287178253 1.172276335245585 0.7118514688347454 0.04596669908479134 2.160216781880242 -0.4558470112795174 -0.1408698236013977 2.55863514213935 -0.9762323021069998 0.3023059074783826 0.1006358104014331 1.798240854639 1.796303359312389 0.2854804374023457 1.695153128919529 -0.7461161123527316 -0.5717889809615559 -1.22969188461553 -0.3124307656341108 -0.4572231441070826 2.196150179180091 0.9920452455196852 0.7298941327650366 -0.009138175032543883 1.196326721003037 3.029519138874344 2.053193497411489 0.3020035894567694 1.885130148538693 0.3571024527100635 -0.006202951752455332 2.422152014091419 1.953976188093006 -0.3035872780193656 1.038990844201456 1.15759942528698 0.8840631785390038 1.006992878003682 0.7844353853636307 0.8614644612780162 0.9999999999999998 1.37819707688161 1.451946740030631 -0.2701312811593382 2.470927310260342 0.188450176887656 1.552249295530555 -0.4011035144371413 0.4812612270534884 3.533385275058706 0.01878389160958915 2.03329739376812 1.497360243363507 0.01291962460839935 2.441040065770387 2.45750172056598 1.759298261864295 1.828459550040343 0.1185298953877549 0.7656071379341856 1.303441760675321 0.3049733552767356 0.3402213592635629 2.653472185103094 0.5024574596798626 1.455823625970243 0.2952856940037123 1.716812084833785 1.01761544215552 -0.06454420761699553 -0.1136659519769139 0.7720535027210765 0.2077001368790136 2.906882616021471 0.5692803618983897 1.402678021067997 1.281703298994574 1.312617838849445 2.571754608952619 1.30457840948769 0.302675318800333 1.14007511822789 2.200676495613032 0.02502240784087517 1.402893483424553 2.061369194121731 2.430616761925077 1.019909066034401 1.392911511673118 1.880834072596762 1.65887993618586 1.854477465005829 1.581964280941516 0.4524338473246255 1.055389551643304 1.698333284152199 0.09466761685818237 1.682866360474479 0.9203714703780533 0.7883175376961334 0.7735400964254449 3.412029603373202 0.9389264766934377 -0.2286519426867513 1.144899531664923 1.959607319634346 0.6820149079212618 0.3659211651228045 1.24457081853047 1.240565634184464 -0.808996907369538 -0.4095289441050487 -0.03984319060471475 1.407376888599285 2.50506971416251 0.94866459792695 1.377655556676116 0.8621283462953613 1.45572695894971 1.337481186615599 -0.03581875865121709 0.6192721038839956 3.077072925413614 1.175569494850847 0.5891259026809106 0.8610329904326544 2.435659738681764 -0.6747189852569966 2.031022199948546 0.5566425448896293 1.447685798202472 -0.02473867507343686 2.007858891101517 1.263674437306774 1.713629327117102 1.254938386649121 1.892944756136862 0.4684330377337796 -0.4027056816821775 1.11704801106262 -1.414532689933904 0.9485616889624722 2.358307923865745 0.04632719508016703 -0.6085524733548022 -0.3156332989897632 -0.3994652220730563 -2.026887037281629 -0.7817493723936182 0.0836673467640805 -0.3829577698394881 1.29819866462604 1.084558245499732 0.9864835029701788 1.119071535438783 -0.1405294307137892 2.214607285013491 -0.006327316715517561 2.212590467370691 1.26588548344322 -0.5429082556043867 0.09490211216100741 2.549335338093492 0.8121250547881319 2.174788321516417 0.6011032880870217 2.834863443688122 1.019969423481439 1.189008892235732 3.880782990535681 -0.4613835224709339 1.30080684608793 0.3341630366858428 2.558988066567454 -0.3541816158487625 1.992892726585691 1.57301742825892 0.7891172056259123 1.335270370293899 1.099317126413363 1.94850228775292 0.7180743145007634 0.2732012480611574 1.88701768090489 0.919039111854304 0.5164906832658123 -1.035409137727921 1.531827498628754 0.8721386145599138 2.057111508572006 0.5829095702425009 1.219918106522843 1.910275635648726 -0.6316897441802187 -0.5740899163490216 -0.4301836663620986 1.433373775047487 0.1971735647599688 1.101488459356943 1.066928958382982 0.6455351033321124 -0.546989207700902 2.366065132501835 3.587260717546521 1.157362400953183 2.326308940608198 3.695299542189807 1.718391676775385 -0.5572607835669456 1.774638640170457 0.6427803541090709 0.8179656256527835 -1.427179248433601 2.347050988513118 0.6939815297602763 1.225605599505495 -0.9044219759169925 -1.142731033609628 1.418193974248724 1.178306680323816 -1.201815224697584 0.04591899739153427 -0.02887756889841087 2.140995206014014 1.095581224518306 -0.2437858265225779 -0.5795306559652984 -0.5941933636597605 1.547264303474973 -1.642462107162598 0.6960281312476969 -0.008382857825509848 -0.725508676535545 1.51600770180333 1.539926920247545 1.388250699055266 2.224010740525287 1.649318957661719 -0.6147625607082601 0.5515733586034677 0.558258455676736 0.7189388536036069 0.5158843706883898 2.153889332982802 0.3828076118746291 0.3677019184304079 1.659632545441951 -0.8225887804579344 1.748981685320509 1.426549705601257 1.406016845030578 0.8899866163193882 0.6809714511623861 2.427223674550403 3.466490892897378 1.342136503754375 0.06841851946696453 1.373415909361131 0.7652623797518443 0.8396093766790385 1.938274360789231 1.904246521995326 -0.03650121825884889 0.1027546072033909 1.566785952884902 3.06898052727411 1.214927572139952 1.050208409427337 2.976858768261151 0.8105352839625762 0.805432928329994 0.9354516434670886 -1.487850555829779 0.5383045120858965 1.236632524549359 2.061736620589021 1.234899946130458 1.795316488842313 0.6678628022755861 -0.4519810377511588 -0.02987276372091996 1.375123105925975 1.315580464196835 1.022030750123769 0.09034629544573369 1.469521469802323 1.748079437898673 0.4963024850608647 0.7332648207644714 1.335852841324077 0.6439092622804476 0.6781507496429851 0.6543782683697399 1.668742462695305 -0.04875535039821921 -1.214398420486452 2.354469182113136 0.03891042224179864 0.6134475222352157 0.4336358043279851 1.326013407781256 -0.117168836983129 1.759926434163588 0.4791213767042255 0.4365430457578929 0.5690695258146682 -0.0531786578714506 2.367319974594577 0.4491257894231035 1.385175892949472 0.7138088403476408 0.2064240103282239 0.2015159906384059 0.5934245273683694 -0.6713871540982557 1.027224205460117 2.552271735323127 1.65150506372262 1.904433424822259 -0.5620851860805303 0.06669369871967024 -0.819153914633457 0.7998890249813373 1.20972782458538 -0.6143752935638263 1.224218276521915 1.52530528404342 1.693008208210148 -0.7709484090466059 1.598971350634212 2.244904936766772 0.3058929035697816 1.227014894672552 1.12644227640822 1.667369525656748 -0.7886982200436146 0.5671573707196975 0.2960941368496872 0.9718998931972267 1.1431859413311 -0.6172018382535209 2.131221940354825 0.8101330855984981 0.2044766135092729 2.920543355522538 1.728484134270207 -0.3428597341096586 1.867400758332775 2.142384246429031 1.39864944981526 1.782769914485578 1.48733335784685 1.069399026465054 0.8236770291736946 1.895725315388256 1.183307434295155 1.687756226992983 1.816280365106959 1.541983281114427 0.674184937519053 1.155707182973492 0.8896722420006936 1.348739935468164 0.07945443115763218 0.4015850998285639 0.2940033118937194 0.8144776439829758 1.010277227746017 1.685498799452889 -0.838982275528326 -1.18018020407638 -0.868412444924779 0.7400610340786427 1.707052905550757 1.919288250721198 1.251273405114258 0.4644987613273633 1.334692331775329 2.538605205036323 2.485722427011433 0.856324109447091 -0.7136542582953915 2.825465101313566 0.08694272044256701 -0.4285086279494847 -0.2502875438659184 2.437339001762728 3.023974046395975 0.8106101645444662 0.6521151035084938 0.09644813760464621 0.06353816989227168 2.161572265357913 -0.9281891337178096 0.8322866265625576 -0.6624871844665274 2.055961578390782 0.01587797975935534 0.7997631265641372 0.4317059172430303 1.531143804662626 2.562595209034343 0.1901457709002661 0.5061057646508257 0.5780694614211119 -0.5401165798621181 -0.8177535916998482 1.866859409187512 0.6109486305688941 1.11376363312526 0.03183485263470687 0.8165023556504525 0.8693515787128633 1.734754815439754 0.1004730547138618 0.6178010884581566 0.4824611720210606 1.182049877268903 -1.018185671997708 1.585419468001058 1.357963678117527 1.397876718473706 -0.2619430180440445 1.005324640919879 0.7964759685407805 2.099693653056788 -0.3755007476680732 0.8312975399984183 1.396105881808023 -0.1896598582949165 2.002961271703618 3.583366494426321 1.330851465240229 1.130103694295259 0.7688013635923769 -0.1286103296916599 -0.1434832589799135 1.643611062741993 1.121930289267041 0.7369263085398459 -0.399645103166467 0.574005355245518 0.5202189972705118 2.985653630789157 2.072293741185865 1.07616339901463 -0.1765205754710266 -0.7930859474858163 1.157926218550625 0.7843350672317007 0.4196496832593138 -0.07300266330033933 1.438911595136333 2.064411160627518 2.726359221689226 2.54805504911209 1.100018848512457 1.043593019354887 1.653632122087634 1.080919973623599 3.385779270214321 1.683797773663598 0.9532190916410197 2.40329493259908 1.283772772485365 1.67695018692607 1.787999220336419 0.7531209642719869 1.65533954344822 0.3041272538354668 2.703140157448808 2.865577528054054 1.083660166482454 3.183503862026781 0.1176591644524645 0.2033963524096726 0.7949491847411757 0.923829221952576 1.324175554457839 -0.8785271277470366 -0.2022031237603816 1.731298686205138 0.9075127909346693 1.47241382695859 2.765055927301861 1.662551233574523 2.229702341089688 2.420339239959411 0.01856912204413241 2.379803526212374 1.194682883418982 0.7314613660992719 2.619104084085683 1.194266390038433 1.755229084507919 -0.7717968320741864 2.775922639288721 3.426301255877113 2.261015872096904 0.651964538515456 1.345939947315443 0.2105043028449838 0.3328698838274554 3.642706438211579 -0.3904994645123505 0.9844749844701601 0.2924099967396088 0.06560500662064384 -0.06615896167074142 -0.7604495935580171 0.4200986436670405 1.442699027263202 0.4610606186888362 1.921740459008894 2.176654783825957 0.2766904269817657 0.7407258065608351 0.1817407648628061 0.9644631817009489 0.6065483491804464 0.2324125184457826 1.274911601852321 -0.8986056273266818 1.404120595961857 0.04156799293478086 2.287418670386505 2.05840669246076 1.799679819125657 2.004151444242522 0.6644434702942055 -0.1966604233793228 0.8685309527545859 0.8165934975554585 1.953237274081445 2.042743234387201 0.2318316739303269 1.288867345651097 0.9215959041223284 -0.07665186592476791 -0.8030549321239862 0.3653138225572382 1.381729284002746 2.190014342076591 0.688056952439958 0.5071921430901954 -0.6041002388472794 1.459369139552627 -0.8416502124388883 1.001052745331846 1.596638837915149 0.5348346491908362 0.2901786382189061 1.342720144385798 1.589092844018214 -0.2283721036511688 0.8305215735059919 2.165485152240601 -0.03252788961942565 0.02328071227927819 1.746912156602784 2.051175246634993 1.914325258851608 -0.2794919436484127 1.426645034300429 0.6929458022750473 1.394410764453883 1.284846736041881 1.090896414615004 -0.7625203833382994 3.081651907582955 0.7665151300770845 0.6821018419088138 0.5603338674426759 0.6572856151612331 1.392476589756923 -0.04647067345709188 1.08147339871604 0.7078394909697348 0.01086012000244907 0.1384793254931365 2.043919155687828 0.4134384610983507 -0.7080618667851521 0.2352748837640597 1.812832124001387 -0.1268862569762619 1.685355392529269 -0.144857368553704 0.8889031624158457 1.949061940584198 1.768733336991409 0.7623401939543458 0.767937305445493 0.6550831207704331 0.3362298871685868 0.7764073163080257 2.359765159287158 1.429089373463981 1.309205351542116 -0.3268723432746896 0.8104497326422911 2.956965849238324 0.358524762718711 1.589412679232686 2.615427889946867 1.610693624201783 1.55191690831084 2.809198581573606 1.330755065953391 -0.1283170853071223 -0.474750683055517 0.9803923739926476 1.346235159202204 1.392911694970242 0.6346043347252104 2.370365713880602 2.698217365974108 0.001521859597514053 0.7471014323465719 2.117297049631915 2.928504152930254 1.908277373323209 1.500462929674708 1.133317338887011 1.507896432518121 2.807132121248947 2.374119900294267 0.907002995671104 0.6909785110621898 -0.6472724969890027 1.797647281265343 0.9467178409047315 -0.6648896706502492 1.530229712113611 0.7206469850455717 -0.5946598393887041 1.456691133146709 2.248258506550396 0.170271558722038 1.831513988086103 -0.3025803355234244 2.385418663428565 1.146733367404714 2.170143805859415 0.4245995483641243 0.949917907426168 0.3102974373794284 1.715536410523382 3.290316642569182 1.242702262755937 -0.297971103458347 1.369721796531666 -0.1401078494450936 1.074358209119162 -0.248762374937652 0.7741944473547974 0.514507383681623 1.787839763152484 0.4533666972649788 0.3519589345259729 2.036886889178933 0.9996729504490831 2.289381718120088 0.431587546009627 -0.5237002243209679 1.239567952522294 0.6761313996158718 0.9071710058567745 0.8837286842961545 1.035352586794545 2.144365426158453 0.4783708236729683 2.033672969617412 1.801519424099742 -0.8344022765758961 0.5602884656419493 0.72107370598711 -0.1512500608802831 -0.8109086017955673 1.763530583698427 -1.176822767358457 -0.4942355480811056 1.161308989334149 1.222869381750991 2.659789397277532 1.425338756687175 2.404180720178092 0.2384537595927544 2.212845748940844 0.5550363648913921 1.524028774206119 0.5981836136185896 0.5294540428864375 0.8696654600401873 0.7833674134167391 1.508798572309083 0.2534079338862683 0.66946204116125 0.1919327690165185 2.137979101543531 1.811537552181822 2.211414480126396 -0.3667803731405608 1.898889228322251 1.622418177467973 1.947201617655731 1.481833835653183 0.601764812275871 0.6303918701726243 1.51994258186477 -0.1161953258017661 2.024617398746776 1.422339904094752 1.183154710585878 0.9304865918550239 1.071618644818221 0.7033879178281861 1.568412859625416 -0.9750664306465477 0.7493605057505957 1.668917742417485 1.767266362343072 1.722993495766987 1.387423726793022 -0.2828739000600107 0.8299562156869035 1.21918696434744 1.01931148214656 1.310285271474827 -0.9664421259477922 1.024933466312315 1.422898978159024 1.566950347807346 0.9021197631092972 0.6222499071420458 0.1321155481729535 1.288063095180657 1.352656771706024 0.1895061657905364 0.1920373324287531 1.478312968404502 0.6999690645255463 1.718244503797262 1.693025225446638 -0.07355066568929547 1.324113730690591 -0.8910892301905946 0.08014974002211994 1.374078902490329 2.627495388780011 -1.307638481303528 -0.3934299458205341 1.611516609206224 0.9667655239515172 0.597723163117555 0.4400589500081084 0.7293240539552928 1.285119876893175 2.336139525648818 -0.6673365593984886 1.530074485490484 0.2171340933522691 -0.0446354765064918 -0.1038020661746528 -0.1819091484153785 0.8678733854163624 1.232817380535235 0.9167791661759996 1.168013108607712 1.493390515296723 1.536417437632474 0.6797286763461281 0.9035159224287149 -0.8191069059672471 0.6012359607464048 1.344444829105806 0.8183485917139273 1.614778215988197 2.223568213372829 0.3734889189821574 1.422649302463133 0.9888487551982762 -1.496709144298501 0.8646479649906167 1.160002618343647 0.6743586761693734 0.6734460402057004 -0.3863713742678794 -0.9604983793458164 2.461399454123412 1.003077847052847 0.2592339470695195 1.569928793084649 0.6382274482858993 -0.9429592775201137 -0.4581566653767766 0.4315195019696789 2.195549098550692 1.235049479489091 2.840815442383532 0.9410390060244023 2.256518702798669 1.234668114148285 0.9465881972606696 -0.5457540293086707 0.162386254249927 0.534006432843394 2.233921297718268 -0.08698581368483005 1.674617042026495 -0.1836532309896213 3.14122995765425 1.433464763688435 -0.206909665367156 1.444601979938507 1.503067169865708 0.08666750927248379 0.03915375698533374 1.837347527702306 1.064527516722064 2.244462190723304 1.617210464548076 1.267451644905893 0.9200161026441995</pre>
 
=={{header|AutoHotkey}}==
contributed by Laszlo on the ahk
[http://www.autohotkey.com/forum/post-276261.html#276261 forum]
<langsyntaxhighlight AutoHotkeylang="autohotkey">Loop 40
R .= RandN(1,0.5) "`n" ; mean = 1.0, standard deviation = 0.5
MsgBox %R%
Line 98 ⟶ 96:
}
Return Y
}</langsyntaxhighlight>
 
=={{header|Avail}}==
<langsyntaxhighlight Availlang="avail">Method "U(_,_)" is
[
lower : number,
Line 133 ⟶ 131:
// the default distribution has mean 0 and std dev 1.0, so we scale the values
sampler ::= map a Marsaglia polar sampler through [d : double | d ÷ 2.0 + 1.0];
values ::= take 1000 from sampler;</langsyntaxhighlight>
 
=={{header|AWK}}==
'''One-liner:'''
<langsyntaxhighlight lang="awk">$ awk 'func r(){return sqrt(-2*log(rand()))*cos(6.2831853*rand())}BEGIN{for(i=0;i<1000;i++)s=s" "1+0.5*r();print s}'</langsyntaxhighlight>
 
'''Readable version:'''
<langsyntaxhighlight lang="awk">
function r() {
return sqrt( -2*log( rand() ) ) * cos(6.2831853*rand() )
Line 153 ⟶ 151:
print s
}
</syntaxhighlight>
</lang>
{{out}} first few values only
<pre>
Line 160 ⟶ 158:
 
=={{header|BASIC}}==
==={{header|ANSI BASIC}}===
{{works with|QuickBasic|4.5}}
{{trans|FreeBASIC}}
RANDOMIZE TIMER 'seeds random number generator with the system time
{{works with|Decimal BASIC}}
pi = 3.141592653589793#
<syntaxhighlight lang="basic">
DIM a(1 TO 1000) AS DOUBLE
100 REM Random numbers
CLS
110 RANDOMIZE
FOR i = 1 TO 1000
120 DEF a(i)RandomNormal = 1 + SQRCOS(-2 * LOG(PI * RND)) * COSSQR(-2 * pi * LOG(RND))
130 DIM R(0 TO 999)
NEXT i
140 LET Sum = 0
150 FOR I = 0 TO 999
160 LET R(I) = 1 + RandomNormal / 2
170 LET Sum = Sum + R(I)
180 NEXT I
190 LET Mean = Sum / 1000
200 LET Sum = 0
210 FOR I = 0 TO 999
220 LET Sum = Sum + (R(I) - Mean) ^ 2
230 NEXT I
240 LET SD = SQR(Sum / 1000)
250 PRINT "Mean is "; Mean
260 PRINT "Standard Deviation is"; SD
270 PRINT
280 END
</syntaxhighlight>
{{out}} Two runs.
<pre>
Mean is 1.00216454061435
Standard Deviation is .504515904812839
</pre>
<pre>
Mean is .995781408878628
Standard Deviation is .499307289407576
</pre>
 
==={{header|BBCApplesoft BASIC}}===
The [[Random_numbers#Commodore_BASIC|Commodore BASIC]] code works in Applesoft BASIC.
<lang bbcbasic> DIM array(999)
 
==={{header|BASIC256}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="basic256"># Generates normally distributed random numbers with mean 0 and standard deviation 1
function randomNormal()
return cos(2.0 * pi * rand) * sqr(-2.0 * log(rand))
end function
 
dim r(1000)
sum = 0.0
# Generate 1000 normally distributed random numbers
# with mean 1 and standard deviation 0.5
# and calculate their sum
for i = 0 to 999
r[i] = 1.0 + randomNormal() / 2.0
sum += r[i]
next i
 
mean = sum / 1000.0
sum = 0.0
# Now calculate their standard deviation
for i = 0 to 999
sum += (r[i] - mean) ^ 2.0
next i
sd = sqr(sum/1000.0)
 
print "Mean is "; mean
print "Standard Deviation is "; sd
end</syntaxhighlight>
{{out}}
<pre>Mean is 1.002092
Standard Deviation is 0.4838570687</pre>
 
==={{header|BBC BASIC}}===
<syntaxhighlight lang="bbcbasic"> DIM array(999)
FOR number% = 0 TO 999
array(number%) = 1.0 + 0.5 * SQR(-2*LN(RND(1))) * COS(2*PI*RND(1))
Line 180 ⟶ 238:
PRINT "Mean = " ; mean
PRINT "Standard deviation = " ; stdev</langsyntaxhighlight>
{{out}}
<pre>Mean = 1.01848064
Standard deviation = 0.503551814</pre>
 
==={{header|Chipmunk Basic}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="basic">
10 ' Random numbers
20 randomize timer
30 dim r(999)
40 sum = 0
50 for i = 0 to 999
60 r(i) = 1+randomnormal()/2
70 sum = sum+r(i)
80 next
90 mean = sum/1000
100 sum = 0
110 for i = 0 to 999
120 sum = sum+(r(i)-mean)^2
130 next
140 sd = sqr(sum/1000)
150 print "Mean is ";mean
160 print "Standard Deviation is ";sd
170 print
180 end
 
500 sub randomnormal()
510 randomnormal = cos(2*pi*rnd(1))*sqr(-2*log(rnd(1)))
520 end sub
</syntaxhighlight>
{{out}}
Two runs.
<pre>
Mean is 1.007087
Standard Deviation is 0.496848
</pre>
<pre>
Mean is 0.9781
Standard Deviation is 0.508147
</pre>
 
==={{header|Commodore BASIC}}===
<syntaxhighlight lang="bbcbasic">
10 DIM AR(999): DIM DE(999)
20 FOR N = 0 TO 999
30 AR(N)= 0 + SQR(-1.3*LOG(RND(1))) * COS(1.2*PI*RND(1))
40 NEXT N
50 :
60 REM SUM
70 LET SU = 0
80 FOR N = 0 TO 999
90 LET SU = SU + AR(N)
100 NEXT N
110 :
120 REM MEAN
130 LET ME= 0
140 LET ME = SU/1000
150 :
160 REM DEVIATION
170 FOR N = 0 TO 999
180 T = AR(N)-ME: REM SUBTRACT MEAN FROM NUMBER
190 T = T * T: REM SQUARE THE RESULT
200 DE(N) = T : REM STORE IN DEVIATION ARRAY
210 NEXT N
220 LET DS=0: REM SUM OF DEVIATION ARRAY
230 FOR N = 0 TO 999
240 LET DS = DS + DE(N)
250 NEXT N
260 LET DM=0: REM MEAN OF DEVIATION ARRAY
270 LET DM = DS / 1000
280 LET DE = 0:
290 LET DE = SQR(DM)
300 :
310 PRINT "MEAN = "ME
320 PRINT "STANDARD DEVIATION ="DE
330 END
</syntaxhighlight>
 
==={{header|FreeBASIC}}===
<syntaxhighlight lang="freebasic">' FB 1.05.0 Win64
 
Const pi As Double = 3.141592653589793
Randomize
 
' Generates normally distributed random numbers with mean 0 and standard deviation 1
Function randomNormal() As Double
Return Cos(2.0 * pi * Rnd) * Sqr(-2.0 * Log(Rnd))
End Function
 
Dim r(0 To 999) As Double
Dim sum As Double = 0.0
 
' Generate 1000 normally distributed random numbers
' with mean 1 and standard deviation 0.5
' and calculate their sum
For i As Integer = 0 To 999
r(i) = 1.0 + randomNormal/2.0
sum += r(i)
Next
 
Dim mean As Double = sum / 1000.0
 
Dim sd As Double
sum = 0.0
' Now calculate their standard deviation
For i As Integer = 0 To 999
sum += (r(i) - mean) ^ 2.0
Next
sd = Sqr(sum/1000.0)
 
Print "Mean is "; mean
Print "Standard Deviation is"; sd
Print
Print "Press any key to quit"
Sleep</syntaxhighlight>
Sample result:
{{out}}
<pre>
Mean is 1.000763573902885
Standard Deviation is 0.500653063426955
</pre>
 
==={{header|FutureBasic}}===
Note: To generate the random number, rather than using FB's native "rnd" function, this code wraps C code into the RandomZeroToOne function.
<syntaxhighlight lang="futurebasic">window 1
 
local fn RandomZeroToOne as double
double result
cln result = (double)( (rand() % 100000 ) * 0.00001 );
end fn = result
 
local fn RandomGaussian as double
double r = fn RandomZeroToOne
end fn = 1 + .5 * ( sqr( -2 * log(r) ) * cos( 2 * pi * r ) )
 
long i
double mean, std, a(1000)
 
for i = 1 to 1000
a(i) = fn RandomGaussian
mean += a(i)
next
mean = mean / 1000
 
for i = 1 to 1000
std += ( a(i) - mean )^2
next
std = std / 1000
 
print " Average: "; mean
print "Standard Deviation: "; std
 
HandleEvents</syntaxhighlight>
{{output}}
<pre>
Average: 1.053724951604593
Standard Deviation: 0.2897370762627166
</pre>
 
==={{header|GW-BASIC}}===
The [[Random_numbers#Commodore_BASIC|Commodore BASIC]] code works in GW-BASIC.
 
==={{header|Liberty BASIC}}===
<syntaxhighlight lang="lb">dim a(1000)
mean =1
sd =0.5
for i = 1 to 1000 ' throw 1000 normal variates
a( i) =mean +sd *( sqr( -2 * log( rnd( 0))) * cos( 2 * pi * rnd( 0)))
next i</syntaxhighlight>
 
==={{header|Minimal BASIC}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="basic">
10 REM Random numbers
20 LET P = 4*ATN(1)
30 RANDOMIZE
40 DEF FNN = COS(2*P*RND)*SQR(-2*LOG(RND))
50 DIM R(999)
60 LET S = 0
70 FOR I = 0 TO 999
80 LET R(I) = 1+FNN/2
90 LET S = S+R(I)
100 NEXT I
110 LET M = S/1000
120 LET S = 0
130 FOR I = 0 TO 999
140 LET S = S+(R(I)-M)^2
150 NEXT I
160 LET D = SQR(S/1000)
170 PRINT "Mean is "; M
180 PRINT "Standard Deviation is"; D
190 PRINT
200 END
</syntaxhighlight>
 
==={{header|PureBasic}}===
<syntaxhighlight lang="purebasic">Procedure.f RandomNormal()
; This procedure can return any real number.
Protected.f x1, x2
 
; random numbers from the open interval ]0, 1[
x1 = (Random(999998)+1) / 1000000 ; must be > 0 because of Log(x1)
x2 = (Random(999998)+1) / 1000000
 
ProcedureReturn Sqr(-2*Log(x1)) * Cos(2*#PI*x2)
EndProcedure
 
Define i, n=1000
 
Dim a.q(n-1)
For i = 0 To n-1
a(i) = 1 + 0.5 * RandomNormal()
Next</syntaxhighlight>
 
==={{header|QuickBASIC}}===
{{works with|QuickBasic|4.5}}
<syntaxhighlight lang="qbasic">
RANDOMIZE TIMER 'seeds random number generator with the system time
pi = 3.141592653589793#
DIM a(1 TO 1000) AS DOUBLE
CLS
FOR i = 1 TO 1000
a(i) = 1 + SQR(-2 * LOG(RND)) * COS(2 * pi * RND)
NEXT i
</syntaxhighlight>
 
==={{header|Run BASIC}}===
<syntaxhighlight lang="runbasic">dim a(1000)
pi = 22/7
for i = 1 to 1000
a( i) = 1 + .5 * (sqr(-2 * log(rnd(0))) * cos(2 * pi * rnd(0)))
next i</syntaxhighlight>
 
==={{header|TI-83 BASIC}}===
Built-in function: randNorm()
randNorm(1,.5)
 
Or by a program:
 
Calculator symbol translations:
 
"STO" arrow: &#8594;
 
Square root sign: &#8730;
 
ClrList L<sub>1</sub>
Radian
For(A,1,1000)
√(-2*ln(rand))*cos(2*π*A)→L<sub>1</sub>(A)
End
 
==={{header|ZX Spectrum Basic}}===
Here we have converted the QBasic code to suit the ZX Spectrum:
<syntaxhighlight lang="zxbasic">10 RANDOMIZE 0 : REM seeds random number generator based on uptime
20 DIM a(1000)
30 CLS
40 FOR i = 1 TO 1000
50 LET a(i) = 1 + SQR(-2 * LN(RND)) * COS(2 * PI * RND)
60 NEXT i</syntaxhighlight>
 
=={{header|C}}==
<langsyntaxhighlight lang="c">#include <stdlib.h>
#include <math.h>
#ifndef M_PI
Line 207 ⟶ 521:
rands[i] = 1.0 + 0.5*random_normal();
return 0;
}</langsyntaxhighlight>
 
=={{header|C sharp}}==
{{trans|JavaScript}}
<langsyntaxhighlight lang="csharp">
private static double randomNormal()
{
return Math.Cos(2 * Math.PI * tRand.NextDouble()) * Math.Sqrt(-2 * Math.Log(tRand.NextDouble()));
}
</syntaxhighlight>
</lang>
 
Then the methods in [[Random numbers#Metafont]] are used to calculate the average and the Standard Deviation:
<langsyntaxhighlight lang="csharp">
static Random tRand = new Random();
 
Line 247 ⟶ 561:
Console.ReadLine();
}
</syntaxhighlight>
</lang>
 
An example result:
Line 260 ⟶ 574:
The new C++ standard looks very similar to the Boost library example below.
 
<langsyntaxhighlight lang="cpp">#include <random>
#include <functional>
#include <vector>
Line 276 ⟶ 590:
generate(v.begin(), v.end(), rnd);
return 0;
}</langsyntaxhighlight>
 
{{works with|C++03}}
<langsyntaxhighlight lang="cpp">#include <cstdlib> // for rand
#include <cmath> // for atan, sqrt, log, cos
#include <algorithm> // for generate_n
Line 305 ⟶ 619:
std::generate_n(array, 1000, normal_distribution(1.0, 0.5));
return 0;
}</langsyntaxhighlight>
 
{{libheader|Boost}}
Line 311 ⟶ 625:
This example used Mersenne Twister generator. It can be changed by changing the typedef.
 
<langsyntaxhighlight lang="cpp">
#include <vector>
#include "boost/random.hpp"
Line 332 ⟶ 646:
return 0;
}
</syntaxhighlight>
</lang>
 
=={{header|Clojure}}==
<langsyntaxhighlight lang="lisp">(import '(java.util Random))
(def normals
(let [r (Random.)]
(take 1000 (repeatedly #(-> r .nextGaussian (* 0.5) (+ 1.0))))))</langsyntaxhighlight>
 
=={{header|COBOL}}==
<syntaxhighlight lang="cobol">
IDENTIFICATION DIVISION.
PROGRAM-ID. RANDOM.
AUTHOR. Bill Gunshannon
INSTALLATION. Home.
DATE-WRITTEN. 14 January 2022.
************************************************************
** Program Abstract:
** Able to get the Mean to be really close to 1.0 but
** couldn't get the Standard Deviation any closer than
** .3 to .4.
************************************************************
DATA DIVISION.
WORKING-STORAGE SECTION.
01 Sample-Size PIC 9(5) VALUE 1000.
01 Total PIC 9(10)V9(5) VALUE 0.0.
01 Arith-Mean PIC 999V999 VALUE 0.0.
01 Std-Dev PIC 999V999 VALUE 0.0.
01 Seed PIC 999V999.
01 TI PIC 9(8).
 
01 Idx PIC 99999 VALUE 0.
01 Intermediate PIC 9(10)V9(5) VALUE 0.0.
01 Rnd-Work.
05 Rnd-Tbl
OCCURS 1 TO 99999 TIMES DEPENDING ON Sample-Size.
10 Rnd PIC 9V9999999 VALUE 0.0.
PROCEDURE DIVISION.
Main-Program.
ACCEPT TI FROM TIME.
MOVE FUNCTION RANDOM(TI) TO Seed.
PERFORM WITH TEST AFTER VARYING Idx
FROM 1 BY 1
UNTIL Idx = Sample-Size
COMPUTE Intermediate =
(FUNCTION RANDOM() * 2.01)
MOVE Intermediate TO Rnd(Idx)
END-PERFORM.
PERFORM WITH TEST AFTER VARYING Idx
FROM 1 BY 1
UNTIL Idx = Sample-Size
COMPUTE Total = Total + Rnd(Idx)
END-PERFORM.
 
 
COMPUTE Arith-Mean = Total / Sample-Size.
DISPLAY "Mean: " Arith-Mean.
 
 
PERFORM WITH TEST AFTER VARYING Idx
FROM 1 BY 1
UNTIL Idx = Sample-Size
COMPUTE Intermediate =
Intermediate + (Rnd(Idx) - Arith-Mean) ** 2
END-PERFORM.
COMPUTE Std-Dev = Intermediate / Sample-Size.
 
 
DISPLAY "Std-Dev: " Std-Dev.
 
STOP RUN.
END PROGRAM RANDOM.
 
</syntaxhighlight>
 
=={{header|Common Lisp}}==
<langsyntaxhighlight lang="lisp">(loop for i from 1 to 1000
collect (1+ (* (sqrt (* -2 (log (random 1.0)))) (cos (* 2 pi (random 1.0))) 0.5)))</langsyntaxhighlight>
 
=={{header|Crystal}}==
{{trans|Phix}}
<langsyntaxhighlight lang="ruby">n, mean, sd, tau = 1000, 1, 0.5, (2 * Math::PI)
array = Array.new(n) { mean + sd * Math.sqrt(-2 * Math.log(rand)) * Math.cos(tau * rand) }
 
mean = array.sum / array.size
standev = Math.sqrt( array.sum{ |x| (x - mean) ** 2 } / array.size )
puts "mean = #{mean}, standard deviation = #{standev}"</langsyntaxhighlight>
{{out}}
<pre>mean = 1.0093442539237896, standard deviation = 0.504694489463623</pre>
 
=={{header|D}}==
<langsyntaxhighlight lang="d">import std.stdio, std.random, std.math;
 
struct NormalRandom {
Line 379 ⟶ 765:
//x = nRnd;
x = nRnd();
}</langsyntaxhighlight>
 
===Alternative Version===
Line 385 ⟶ 771:
{{libheader|tango}}
 
<langsyntaxhighlight lang="d">import tango.math.random.Random;
 
void main() {
Line 394 ⟶ 780:
l = 1.0 + 0.5 * l;
}
}</langsyntaxhighlight>
 
=={{header|Delphi}}==
Line 400 ⟶ 786:
Delphi has RandG function which generates random numbers with normal distribution using Marsaglia-Bray algorithm:
 
<langsyntaxhighlight Delphilang="delphi">program Randoms;
 
{$APPTYPE CONSOLE}
Line 418 ⟶ 804:
Writeln('Std Deviation = ', StdDev(Values):6:4);
Readln;
end.</langsyntaxhighlight>
{{out}}
<pre>Mean = 1.0098
Line 424 ⟶ 810:
 
=={{header|DWScript}}==
<langsyntaxhighlight lang="delphi">var values : array [0..999] of Float;
var i : Integer;
 
for i := values.Low to values.High do
values[i] := RandG(1, 0.5);</langsyntaxhighlight>
 
=={{header|E}}==
<langsyntaxhighlight lang="e">accum [] for _ in 1..1000 { _.with(entropy.nextGaussian()) }</langsyntaxhighlight>
 
=={{header|EasyLang}}==
 
<syntaxhighlight lang="text">
<lang>for i range 1000
numfmt 5 0
a[] &= 1 + 0.5 * sqrt (-2 * logn randomf) * cos (360 * randomf)
e = 2.7182818284590452354
for i = 1 to 1000
a[] &= 1 + 0.5 * sqrt (-2 * log10 randomf / log10 e) * cos (360 * randomf)
.
printfor v in a[]</lang>
avg += v / len a[]
.
print "Average: " & avg
for v in a[]
s += pow (v - avg) 2
.
s = sqrt (s / len a[])
print "Std deviation: " & s
 
</syntaxhighlight>
 
=={{header|Eiffel}}==
<langsyntaxhighlight lang="eiffel">
class
APPLICATION
Line 537 ⟶ 936:
end
end
</syntaxhighlight>
</lang>
 
Example Result
Line 547 ⟶ 946:
=={{header|Elena}}==
{{trans|C#}}
ELENA 46.1x :
<langsyntaxhighlight lang="elena">import extensions;
import extensions'math;
Line 562 ⟶ 961:
real tAvg := 0;
for (int x := 0,; x < a.Length,; x += 1)
{
a[x] := (randomNormal()) / 2 + 1;
Line 572 ⟶ 971:
real s := 0;
for (int x := 0,; x < a.Length,; x += 1)
{
s += power(a[x] - tAvg, 2)
Line 582 ⟶ 981:
console.readChar()
}</langsyntaxhighlight>
{{out}}
<pre>
Line 590 ⟶ 989:
 
=={{header|Elixir}}==
<langsyntaxhighlight lang="elixir">defmodule Random do
def normal(mean, sd) do
{a, b} = {:rand.uniform, :rand.uniform}
Line 605 ⟶ 1,004:
 
xs = for _ <- 1..1000, do: Random.normal(1.0, 0.5)
std_dev.(xs)</langsyntaxhighlight>
{{out}}
<pre>
Line 612 ⟶ 1,011:
 
used Erlang function <code>:rand.normal</code>
<langsyntaxhighlight lang="elixir">xs = for _ <- 1..1000, do: 1.0 + :rand.normal * 0.5
std_dev.(xs)</langsyntaxhighlight>
{{out}}
<pre>
Line 622 ⟶ 1,021:
{{works with|Erlang}}
 
<langsyntaxhighlight lang="erlang">
mean(Values) ->
mean(tl(Values), hd(Values), 1).
Line 654 ⟶ 1,053:
io:format("mean = ~w\n", [mean(X)]),
io:format("stddev = ~w\n", [stddev(X)]).
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 662 ⟶ 1,061:
 
=={{header|ERRE}}==
<syntaxhighlight lang="text">
PROGRAM DISTRIBUTION
 
Line 698 ⟶ 1,097:
 
END PROGRAM
</syntaxhighlight>
</lang>
 
=={{header|Euler Math Toolbox}}==
 
<syntaxhighlight lang="euler math toolbox">
<lang Euler Math Toolbox>
>v=normal(1,1000)*0.5+1;
>mean(v), dev(v)
1.00291801071
0.498226876528
</syntaxhighlight>
</lang>
 
=={{header|Euphoria}}==
{{trans|PureBasic}}
<langsyntaxhighlight lang="euphoria">include misc.e
 
function RandomNormal()
Line 725 ⟶ 1,124:
for i = 1 to n do
s[i] = 1 + 0.5 * RandomNormal()
end for</langsyntaxhighlight>
 
=={{header|F_Sharp|F#}}==
<langsyntaxhighlight lang="fsharp">
let n = MathNet.Numerics.Distributions.Normal(1.0,0.5)
List.init 1000 (fun _->n.Sample())
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 755 ⟶ 1,154:
0.6224640457; 0.8524078517; 0.7646595627; 0.6799834691; 0.773111053; ...]
</pre> =={{header|F_Sharp|F#}}==
<langsyntaxhighlight lang="fsharp">let gaussianRand count =
let o = new System.Random()
let pi = System.Math.PI
let gaussrnd =
(fun _ -> 1. + 0.5 * sqrt(-2. * log(o.NextDouble())) * cos(2. * pi * o.NextDouble()))
[ for i in {0 .. (int count)} -> gaussrnd() ]</langsyntaxhighlight>
 
=={{header|Factor}}==
<langsyntaxhighlight lang="factor">USING: random ;
1000 [ 1.0 0.5 normal-random-float ] replicate</langsyntaxhighlight>
 
=={{header|Falcon|}}==
<langsyntaxhighlight lang="falcon">a = []
for i in [0:1000] : a+= norm_rand_num()
 
Line 773 ⟶ 1,172:
pi = 2*acos(0)
return 1 + (cos(2 * pi * random()) * pow(-2 * log(random()) ,1/2)) /2
end</langsyntaxhighlight>
 
=={{header|Fantom}}==
Line 779 ⟶ 1,178:
Two solutions. The first uses Fantom's random-number generator, which produces a uniform distribution. So, convert to a normal distribution using a formula:
 
<langsyntaxhighlight lang="fantom">
class Main
{
Line 797 ⟶ 1,196:
}
}
</syntaxhighlight>
</lang>
 
The second calls out to Java's Gaussian random-number generator:
 
<langsyntaxhighlight lang="fantom">
using [java] java.util::Random
 
Line 822 ⟶ 1,221:
}
}
</syntaxhighlight>
</lang>
 
=={{header|Forth}}==
{{works with|gforth|0.6.2}}
 
<langsyntaxhighlight lang="forth">require random.fs
here to seed
 
Line 842 ⟶ 1,241:
0 do frnd-normal 0.5e f* 1e f+ f, loop ;
 
create rnd-array 1000 ,normals</langsyntaxhighlight>
 
For newer versions of gforth (tested on 0.7.3), it seems you need to use <tt>HERE SEED !</tt> instead of <tt>HERE TO SEED</tt>, because <tt>SEED</tt> has been made a variable instead of a value.
 
<syntaxhighlight lang="text">rnd rnd dabs d>f</langsyntaxhighlight> is necessary, but surprising and definitely not well documented / perhaps not compliant.
 
=={{header|Fortran}}==
{{works with|Fortran|90 and later}}
<langsyntaxhighlight lang="fortran">PROGRAM Random
 
INTEGER, PARAMETER :: n = 1000
Line 873 ⟶ 1,272:
WRITE(*, "(A,F8.6)") "Standard Deviation = ", sd
 
END PROGRAM Random</langsyntaxhighlight>
 
{{out}}
Line 884 ⟶ 1,283:
Free Pascal provides the '''randg''' function in the RTL math unit that produces Gaussian-distributed random numbers with the Box-Müller algorithm.
 
<langsyntaxhighlight lang="pascal">
function randg(mean,stddev: float): float;
</syntaxhighlight>
</lang>
 
=={{header|FreeBASIC}}==
<lang freebasic>' FB 1.05.0 Win64
 
Const pi As Double = 3.141592653589793
Randomize
 
' Generates normally distributed random numbers with mean 0 and standard deviation 1
Function randomNormal() As Double
Return Cos(2.0 * pi * Rnd) * Sqr(-2.0 * Log(Rnd))
End Function
 
Dim r(0 To 999) As Double
Dim sum As Double = 0.0
 
' Generate 1000 normally distributed random numbers
' with mean 1 and standard deviation 0.5
' and calculate their sum
For i As Integer = 0 To 999
r(i) = 1.0 + randomNormal/2.0
sum += r(i)
Next
 
Dim mean As Double = sum / 1000.0
 
Dim sd As Double
sum = 0.0
' Now calculate their standard deviation
For i As Integer = 0 To 999
sum += (r(i) - mean) ^ 2.0
Next
sd = Sqr(sum/1000.0)
 
Print "Mean is "; mean
Print "Standard Deviation is"; sd
Print
Print "Press any key to quit"
Sleep</lang>
Sample result:
{{out}}
<pre>
Mean is 1.000763573902885
Standard Deviation is 0.500653063426955
</pre>
 
=={{header|Frink}}==
<syntaxhighlight lang="frink">a = new array[[1000], {|x| randomGaussian[1, 0.5]}]</syntaxhighlight>
<lang frink>
a = new array
for i = 1 to 1000
a.push[randomGaussian[1, 0.5]]
</lang>
 
=={{header|FutureBasic}}==
Note: To generate the random number, rather than using FB's native "rnd" function, this code wraps C code into the RandomZeroToOne function.
<lang futurebasic>
include "ConsoleWindow"
 
local fn RandomZeroToOne as double
dim as double result
BeginCCode
result = (double)( (rand() % 100000 ) * 0.00001 );
EndC
end fn = result
 
local fn RandomGaussian as double
dim as double r
 
r = fn RandomZeroToOne
end fn = 1 + .5 * ( sqr( -2 * log(r) ) * cos( 2 * pi * r ) )
 
dim as long i
dim as double mean, std, a(1000)
 
for i = 1 to 1000
a(i) = fn RandomGaussian
mean += a(i)
next
mean = mean / 1000
 
for i = 1 to 1000
std += ( a(i) - mean )^2
next
std = std / 1000
 
print " Average:"; mean
print "Standard Deviation:"; std
</lang>
Output:
<pre>
Average: 1.0258434498
Standard Deviation: 0.2771047023
</pre>
 
=={{header|Go}}==
This solution uses math/rand package in the standard library. See also though the subrepository rand package at https://godoc.org/golang.org/x/exp/rand, which also has a NormFloat64 and has a rand source with a number of advantages over the one in standard library.
<langsyntaxhighlight lang="go">package main
 
import (
Line 1,027 ⟶ 1,337:
fmt.Println(strings.Repeat("*", (c+hscale/2)/hscale))
}
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,047 ⟶ 1,357:
 
=={{header|Groovy}}==
<langsyntaxhighlight lang="groovy">rnd = new Random()
result = (1..1000).inject([]) { r, i -> r << rnd.nextGaussian() }</langsyntaxhighlight>
 
=={{header|Haskell}}==
 
<langsyntaxhighlight lang="haskell">import System.Random
 
pairs :: [a] -> [(a,a)]
Line 1,065 ⟶ 1,375:
 
result :: IO [Double]
result = getStdGen >>= \g -> return $ gaussians 1000 g</langsyntaxhighlight>
 
Or using Data.Random from random-fu package:
<langsyntaxhighlight lang="haskell">replicateM 1000 $ normal 1 0.5</langsyntaxhighlight>
To print them:
<langsyntaxhighlight lang="haskell">import Data.Random
import Control.Monad
 
Line 1,078 ⟶ 1,388:
main = do
x <- sample thousandRandomNumbers
print x</langsyntaxhighlight>
 
=={{header|HicEst}}==
<langsyntaxhighlight lang="hicest">REAL :: n=1000, m=1, s=0.5, array(n)
 
pi = 4 * ATAN(1)
array = s * (-2*LOG(RAN(1)))^0.5 * COS(2*pi*RAN(1)) + m </langsyntaxhighlight>
 
=={{header|Icon}} and {{header|Unicon}}==
Line 1,090 ⟶ 1,400:
 
Note that Unicon randomly seeds it's generator.
<langsyntaxhighlight lang="icon">
procedure main()
local L
Line 1,098 ⟶ 1,408:
every write(!L)
end
</syntaxhighlight>
</lang>
 
=={{header|IDL}}==
<langsyntaxhighlight lang="idl">result = 1.0 + 0.5*randomn(seed,1000)</langsyntaxhighlight>
 
=={{header|J}}==
'''Solution:'''
<langsyntaxhighlight lang="j">urand=: ?@$ 0:
zrand=: (2 o. 2p1 * urand) * [: %: _2 * [: ^. urand
 
1 + 0.5 * zrand 100</langsyntaxhighlight>
 
'''Alternative Solution:'''<br>
Using the normal script from the [[j:Addons/stats/distribs|stats/distribs addon]].
<langsyntaxhighlight lang="j"> require 'stats/distribs/normal'
1 0.5 rnorm 1000
1.44868803 1.21548637 0.812460657 1.54295452 1.2470606 ...</langsyntaxhighlight>
 
=={{header|Java}}==
<langsyntaxhighlight lang="java">double[] list = new double[1000];
double mean = 1.0, std = 0.5;
Random rng = new Random();
for(int i = 0;i<list.length;i++) {
list[i] = mean + std * rng.nextGaussian();
}</langsyntaxhighlight>
 
=={{header|JavaScript}}==
<langsyntaxhighlight lang="javascript">function randomNormal() {
return Math.cos(2 * Math.PI * Math.random()) * Math.sqrt(-2 * Math.log(Math.random()))
}
Line 1,132 ⟶ 1,442:
for (var i=0; i < 1000; i++){
a[i] = randomNormal() / 2 + 1
}</langsyntaxhighlight>
 
=={{header|jq}}==
Line 1,145 ⟶ 1,455:
 
''''A Pseudo-Random Number Generator''''
<langsyntaxhighlight lang="jq"># 15-bit integers generated using the same formula as rand() from the Microsoft C Runtime.
# The random numbers are in [0 -- 32767] inclusive.
# Input: an array of length at least 2 interpreted as [count, state, ...]
Line 1,152 ⟶ 1,462:
.[0] as $count | .[1] as $state
| ( (214013 * $state) + 2531011) % 2147483648 # mod 2^31
| [$count+1 , ., (. / 65536 | floor) ] ;</langsyntaxhighlight>
''''Box-Muller Method''''
<langsyntaxhighlight lang="jq"># Generate a single number following the normal distribution with mean 0, variance 1,
# using the Box-Muller method: X = sqrt(-2 ln U) * cos(2 pi V) where U and V are uniform on [0,1].
# Input: [n, state]
Line 1,173 ⟶ 1,483:
next_rand_normal
| recurse( if .[0] < count then next_rand_normal else empty end)
| .[2] = (.[2] * sd) + mean;</langsyntaxhighlight>
'''Example'''
The task can be completed using: [0,1] | random_normal_variate(1; 0.5; 1000) | .[2]
 
We show just the sample average and standard deviation:
<langsyntaxhighlight lang="jq">def summary:
length as $l | add as $sum | ($sum/$l) as $a
| reduce .[] as $x (0; . + ( ($x - $a) | .*. ))
| [ $a, (./$l | sqrt)] ;
 
[ [0,1] | random_normal_variate(1; 0.5; 1000) | .[2] ] | summary</langsyntaxhighlight>
{{out}}
$ jq -n -c -f Random_numbers.jq
Line 1,190 ⟶ 1,500:
=={{header|Julia}}==
Julia's standard library provides a <code>randn</code> function to generate normally distributed random numbers (with mean 0 and standard deviation 0.5, which can be easily rescaled to any desired values):
<langsyntaxhighlight lang="julia">randn(1000) * 0.5 + 1</langsyntaxhighlight>
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang="scala">// version 1.0.6
 
import java.util.Random
Line 1,206 ⟶ 1,516:
println("Mean is $mean")
println("S.D. is $sd")
}</langsyntaxhighlight>
Sample output:
{{out}}
Line 1,217 ⟶ 1,527:
{{works with|LabVIEW|8.6}}
[[File:LV_array_of_randoms_with_given_mean_and_stdev.png]]
 
=={{header|Liberty BASIC}}==
<lang lb>dim a(1000)
mean =1
sd =0.5
for i = 1 to 1000 ' throw 1000 normal variates
a( i) =mean +sd *( sqr( -2 * log( rnd( 0))) * cos( 2 * pi * rnd( 0)))
next i</lang>
 
=={{header|Lingo}}==
<langsyntaxhighlight Lingolang="lingo">-- Returns a random float value in range 0..1
on randf ()
n = random(the maxinteger)-1
return n / float(the maxinteger-1)
end</langsyntaxhighlight>
 
<langsyntaxhighlight Lingolang="lingo">normal = []
repeat with i = 1 to 1000
normal.add(1 + sqrt(-2 * log(randf())) * cos(2 * PI * randf()) / 2)
end repeat</langsyntaxhighlight>
 
=={{header|Lobster}}==
Uses built-in <code>rnd_gaussian</code>
<syntaxhighlight lang="lobster">
<lang Lobster>
let mean = 1.0
let stdv = 0.5
Line 1,270 ⟶ 1,572:
 
test_random_normal()
</syntaxhighlight>
</lang>
 
=={{header|Logo}}==
{{works with|UCB Logo}}
The earliest Logos only have a RANDOM function for picking a random non-negative integer. Many modern Logos have floating point random generators built-in.
<langsyntaxhighlight lang="logo">to random.float ; 0..1
localmake "max.int lshift -1 -1
output quotient random :max.int :max.int
Line 1,284 ⟶ 1,586:
end
 
make "randoms cascade 1000 [fput random.gaussian / 2 + 1 ?] []</langsyntaxhighlight>
 
=={{header|Lua}}==
<langsyntaxhighlight lang="lua">local list = {}
for i = 1, 1000 do
list[i] = 1 + math.sqrt(-2 * math.log(math.random())) * math.cos(2 * math.pi * math.random()) / 2
end</langsyntaxhighlight>
 
=={{header|M2000 Interpreter}}==
M2000 use a Wichmann - Hill Pseudo Random Number Generator.
<syntaxhighlight lang="m2000 interpreter">
<lang M2000 Interpreter>
Module CheckIt {
Function StdDev (A()) {
Line 1,349 ⟶ 1,651:
}
Checkit
</syntaxhighlight>
</lang>
 
=={{header|Maple}}==
<langsyntaxhighlight lang="maple">with(Statistics):
Sample(Normal(1, 0.5), 1000);</langsyntaxhighlight>
 
'''or'''
 
<langsyntaxhighlight lang="maple">1+0.5*ArrayTools[RandomArray](1000,1,distribution=normal);</langsyntaxhighlight>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
Built-in function RandomReal with built-in distribution NormalDistribution as an argument:
<langsyntaxhighlight Mathematicalang="mathematica">RandomReal[NormalDistribution[1, 1/2], 1000]</langsyntaxhighlight>
 
=={{header|MATLAB}}==
 
Native support :
<langsyntaxhighlight MATLABlang="matlab"> mu = 1; sd = 0.5;
x = randn(1000,1) * sd + mu;
</syntaxhighlight>
</lang>
 
The statistics toolbox provides this function
<langsyntaxhighlight MATLABlang="matlab"> x = normrnd(mu, sd, [1000,1]); </langsyntaxhighlight>
 
This script uses the Box-Mueller Transform to transform a number from the uniform distribution to a normal distribution of mean = mu0 and standard deviation = chi2.
 
<langsyntaxhighlight MATLABlang="matlab">function randNum = randNorm(mu0,chi2, sz)
radiusSquared = +Inf;
Line 1,389 ⟶ 1,691:
randNum = (v .* scaleFactor .* chi2) + mu0;
 
end</langsyntaxhighlight>
 
Output:
<langsyntaxhighlight MATLABlang="matlab">>> randNorm(1,.5, [1000,1])
 
ans =
 
0.693984121077029</langsyntaxhighlight>
 
=={{header|Maxima}}==
<langsyntaxhighlight lang="maxima">load(distrib)$
 
random_normal(1.0, 0.5, 1000);</langsyntaxhighlight>
 
=={{header|MAXScript}}==
 
<langsyntaxhighlight lang="maxscript">arr = #()
for i in 1 to 1000 do
(
Line 1,412 ⟶ 1,714:
c = 1.0 + 0.5 * sqrt (-2*log a) * cos (360*b) -- Maxscript cos takes degrees
append arr c
)</langsyntaxhighlight>
 
=={{header|Metafont}}==
Line 1,418 ⟶ 1,720:
Metafont has <code>normaldeviate</code> which produces pseudorandom normal distributed numbers with mean 0 and variance one. So the following complete the task:
 
<langsyntaxhighlight lang="metafont">numeric col[];
 
m := 0; % m holds the mean, for testing purposes
Line 1,436 ⟶ 1,738:
 
show m, s; % and let's show that really they get what we wanted
end</langsyntaxhighlight>
 
A run gave
Line 1,447 ⟶ 1,749:
 
=={{header|MiniScript}}==
<langsyntaxhighlight MiniScriptlang="miniscript">randNormal = function(mean=0, stddev=1)
return mean + sqrt(-2 * log(rnd,2.7182818284)) * cos(2*pi*rnd) * stddev
end function
Line 1,454 ⟶ 1,756:
for i in range(1,1000)
x.push randNormal(1, 0.5)
end for</langsyntaxhighlight>
 
=={{header|Mirah}}==
<langsyntaxhighlight lang="mirah">import java.util.Random
 
list = double[999]
Line 1,466 ⟶ 1,768:
list[i] = mean + std * rng.nextGaussian
end
</syntaxhighlight>
</lang>
 
=={{header|МК-61/52}}==
<syntaxhighlight lang="text">П7 <-> П8 1/x П6 ИП6 П9 СЧ П6 1/x
ln ИП8 * 2 * КвКор ИП9 2 * пи
* sin * ИП7 + С/П БП 05</langsyntaxhighlight>
 
''Input'': РY - variance, РX - expectation.
Line 1,477 ⟶ 1,779:
Or:
 
<syntaxhighlight lang="text">3 10^x П0 ПП 13 2 / 1 + С/П L0 03 С/П
СЧ lg 2 /-/ * КвКор 2 пи ^ СЧ * * cos * В/О</langsyntaxhighlight>
 
to generate 1000 numbers with a mean of 1.0 and a standard deviation of 0.5.
Line 1,485 ⟶ 1,787:
{{trans|C}}
 
<langsyntaxhighlight lang="modula3">MODULE Rand EXPORTS Main;
 
IMPORT Random;
Line 1,505 ⟶ 1,807:
rands[i] := 1.0D0 + 0.5D0 * RandNorm();
END;
END Rand.</langsyntaxhighlight>
 
=={{header|Nanoquery}}==
{{trans|Java}}
<langsyntaxhighlight lang="nanoquery">list = {0} * 1000
mean = 1.0; std = 0.5
rng = new(Nanoquery.Util.Random)
Line 1,515 ⟶ 1,817:
for i in range(0, len(list) - 1)
list[i] = mean + std * rng.getGaussian()
end</langsyntaxhighlight>
 
=={{header|NetRexx}}==
<langsyntaxhighlight NetRexxlang="netrexx">/* NetRexx */
options replace format comments java crossref symbols nobinary
 
Line 1,573 ⟶ 1,875:
 
return
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,616 ⟶ 1,918:
 
=={{header|NewLISP}}==
<syntaxhighlight lang NewLISP="newlisp">(normal 1 .5 1000)</langsyntaxhighlight>
 
=={{header|Nim}}==
<langsyntaxhighlight lang="nim">import mathrandom, stats, strutilsstrformat
 
var rs: RunningStat
const precisn = 5
var rs: TRunningStat
 
proc normGauss: float {.inline.} = 1 + 0.76 * cos(2*PI*random(1.0)) * sqrt(-2*log10(random(1.0)))
 
randomize()
 
for j_ in 01..5:
for i_ in 01..1000: rs.push gauss(1.0, 0.5)
echo &"mean: {rs.mean:.5f} stdDev: {rs.standardDeviation:.5f}"
rs.push(normGauss())
</syntaxhighlight>
echo("mean: ", $formatFloat(rs.mean,ffDecimal,precisn),
" stdDev: ", $formatFloat(rs.standardDeviation(),ffDecimal,precisn))</lang>
{{out}}
<pre>mean: 1.0170301294 stdDev: 0.5032449692
mean: 1.0118700262 stdDev: 0.5006050028
mean: 10.0021699878 stdDev: 0.4996949662
mean: 10.0033599830 stdDev: 0.5018449820
mean: 1.0012000658 stdDev: 0.4983049703</pre>
mean: 1.00217 stdDev: 0.49911</pre>
 
=={{header|Objeck}}==
<langsyntaxhighlight lang="objeck">bundle Default {
class RandomNumbers {
function : Main(args : String[]) ~ Nil {
Line 1,659 ⟶ 1,956:
}
}
}</langsyntaxhighlight>
 
=={{header|OCaml}}==
<langsyntaxhighlight lang="ocaml">let pi = 4. *. atan 1.;;
let random_gaussian () =
1. +. sqrt (-2. *. log (Random.float 1.)) *. cos (2. *. pi *. Random.float 1.);;
let a = Array.init 1000 (fun _ -> random_gaussian ());;</langsyntaxhighlight>
 
=={{header|Octave}}==
<langsyntaxhighlight lang="octave">p = normrnd(1.0, 0.5, 1000, 1);
disp(mean(p));
disp(sqrt(sum((p - mean(p)).^2)/numel(p)));</langsyntaxhighlight>
 
{{out}}
Line 1,679 ⟶ 1,976:
{{trans|REXX}}
===version 1===
<langsyntaxhighlight lang="oorexx">/*REXX pgm gens 1,000 normally distributed #s: mean=1, standard dev.=0.5*/
pi=RxCalcPi() /* get value of pi */
Parse Arg n seed . /* allow specification of N & seed*/
Line 1,720 ⟶ 2,017:
Return RxCalcPower(_/n,.5)
 
:: requires rxmath library</langsyntaxhighlight>
{{out}}
<pre> old mean= 0.49830002
Line 1,729 ⟶ 2,026:
===version 2===
Using the nice function names in the algorithm.
<langsyntaxhighlight lang="oorexx">/*REXX pgm gens 1,000 normally distributed #s: mean=1, standard dev.=0.5*/
pi=RxCalcPi() /* get value of pi */
Parse Arg n seed . /* allow specification of N & seed*/
Line 1,774 ⟶ 2,071:
sin: Return RxCalcSin(arg(1),,'R')
 
:: requires rxmath library</langsyntaxhighlight>
 
=={{header|PARI/GP}}==
<langsyntaxhighlight lang="parigp">rnormal()={
my(pr=32*ceil(default(realprecision)*log(10)/log(4294967296)),u1=random(2^pr)*1.>>pr,u2=random(2^pr)*1.>>pr);
sqrt(-2*log(u1))*cos(2*Pi*u1u2) \\ in previous version "u1" instead of "u2" was used --> has given crap distribution
\\ Could easily be extended with a second normal at very little cost.
};
vector(1000,unused,rnormal()/2+1)</langsyntaxhighlight>
 
=={{header|Pascal}}==
 
The following function calculates Gaussian-distributed random numbers with the Box-Müller algorithm:
<langsyntaxhighlight lang="pascal">
function rnorm (mean, sd: real): real;
{Calculates Gaussian random numbers according to the Box-Müller approach}
Line 1,795 ⟶ 2,092:
u1 := random;
u2 := random;
rnorm := mean * abs(1 + sqrt(-2 * (ln(u1))) * cos(2 * pi * u2) * sd);
/* error !?! Shouldn't it be "mean +" instead of "mean *" ? */
end;
</syntaxhighlight>
</lang>
 
[[#Delphi | Delphi]] and [[#Free Pascal|Free Pascal]] support implement a '''randg''' function that delivers Gaussian-distributed random numbers.
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">my $PI = 2 * atan2 1, 0;
 
my @nums = map {
1 + 0.5 * sqrt(-2 * log rand) * cos(2 * $PI * rand)
} 1..1000;</langsyntaxhighlight>
 
=={{header|Phix}}==
{{Trans|Euphoria}}
<!--<syntaxhighlight lang="phix">-->
<lang Phix>function RandomNormal()
<span style="color: #008080;">function</span> <span style="color: #000000;">RandomNormal</span><span style="color: #0000FF;">()</span>
return sqrt(-2*log(rnd())) * cos(2*PI*rnd())
<span style="color: #008080;">return</span> <span style="color: #7060A8;">sqrt</span><span style="color: #0000FF;">(-</span><span style="color: #000000;">2</span><span style="color: #0000FF;">*</span><span style="color: #7060A8;">log</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">rnd</span><span style="color: #0000FF;">()))</span> <span style="color: #0000FF;">*</span> <span style="color: #7060A8;">cos</span><span style="color: #0000FF;">(</span><span style="color: #000000;">2</span><span style="color: #0000FF;">*</span><span style="color: #000000;">PI</span><span style="color: #0000FF;">*</span><span style="color: #7060A8;">rnd</span><span style="color: #0000FF;">())</span>
end function
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
sequence s = repeat(0,1000)
<span style="color: #004080;">sequence</span> <span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1000</span><span style="color: #0000FF;">)</span>
for i=1 to length(s) do
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
s[i] = 1 + 0.5 * RandomNormal()
<span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span> <span style="color: #0000FF;">+</span> <span style="color: #000000;">0.5</span> <span style="color: #0000FF;">*</span> <span style="color: #000000;">RandomNormal</span><span style="color: #0000FF;">()</span>
end for</lang>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</syntaxhighlight>-->
 
=={{header|Phixmonti}}==
<langsyntaxhighlight Phixmontilang="phixmonti">include ..\Utilitys.pmt
 
def RandomNormal
Line 1,838 ⟶ 2,138:
get mean - 2 power rot + swap
endfor
swap n / sqrt "Standard deviation: " print print</langsyntaxhighlight>
 
=={{header|PHP}}==
 
<langsyntaxhighlight lang="php">function random() {
return mt_rand() / mt_getrandmax();
}
Line 1,852 ⟶ 2,152:
$a[$i] = 1.0 + ((sqrt(-2 * log(random())) * cos(2 * $pi * random())) * 0.5);
}</langsyntaxhighlight>
 
=={{header|Picat}}==
<syntaxhighlight lang="picat">main =>
_ = random2(), % random seed
G = [gaussian_dist(1,0.5) : _ in 1..1000],
println(first_10=G[1..10]),
println([mean=avg(G),stdev=stdev(G)]),
nl.
 
% Gaussian (Normal) distribution, Box-Muller algorithm
gaussian01() = Y =>
U = frand(0,1),
V = frand(0,1),
Y = sqrt(-2*log(U))*sin(2*math.pi*V).
 
gaussian_dist(Mean,Stdev) = Mean + (gaussian01() * Stdev).
 
% Variance of Xs
variance(Xs) = Variance =>
Mu = avg(Xs),
N = Xs.len,
Variance = sum([ (X-Mu)**2 : X in Xs ]) / N.
 
% Standard deviation
stdev(Xs) = sqrt(variance(Xs)).</syntaxhighlight>
 
{{out}}
<pre>first_10 = [1.639965415776091,0.705425965005482,0.981532402477848,0.309148743347499,1.252800181962738,0.098829881195179,0.74888084504147,0.181494956495445,1.304931340021904,0.595939453660087]
[mean = 0.99223677282248,stdev = 0.510336641737154]</pre>
 
 
=={{header|PicoLisp}}==
{{trans|C}}
<langsyntaxhighlight PicoLisplang="picolisp">(load "@lib/math.l")
 
(de randomNormal () # Normal distribution, centered on 0, std dev 1
Line 1,871 ⟶ 2,201:
(link (+ 1.0 (/ (randomNormal) 2))) ) )
(for N (head 7 Result) # Print first 7 results
(prin (format N *Scl) " ") ) )</langsyntaxhighlight>
{{out}}
<pre>1.500334 1.212931 1.095283 0.433122 0.459116 1.302446 0.402477</pre>
 
=={{header|PL/I}}==
<syntaxhighlight lang="pl/i">
<lang PL/I>
/* CONVERTED FROM WIKI FORTRAN */
Normal_Random: procedure options (main);
Line 1,898 ⟶ 2,228:
put skip edit ( "Standard Deviation = ", sd) (a, F(18,16));
END Normal_Random;
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,909 ⟶ 2,239:
 
=={{header|PL/SQL}}==
<syntaxhighlight lang="pl/sql">
<lang PL/SQL>
DECLARE
--The desired collection
Line 1,925 ⟶ 2,255:
END LOOP;
END;
</syntaxhighlight>
</lang>
 
=={{header|Pop11}}==
<langsyntaxhighlight lang="pop11">;;; Choose radians as arguments to trigonometic functions
true -> popradians;
 
Line 1,942 ⟶ 2,272:
for i from 1 to 1000 do 1.0+0.5*random_normal() endfor;
;;; collect them into array
consvector(1000) -> array;</langsyntaxhighlight>
 
=={{header|PowerShell}}==
Equation adapted from Liberty BASIC
<langsyntaxhighlight lang="powershell">function Get-RandomNormal
{
[CmdletBinding()]
Line 1,981 ⟶ 2,311:
 
$Stats | Format-List
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,988 ⟶ 2,318:
StandardDeviation : 0.489099623426272
</pre>
 
=={{header|PureBasic}}==
<lang PureBasic>Procedure.f RandomNormal()
; This procedure can return any real number.
Protected.f x1, x2
 
; random numbers from the open interval ]0, 1[
x1 = (Random(999998)+1) / 1000000 ; must be > 0 because of Log(x1)
x2 = (Random(999998)+1) / 1000000
 
ProcedureReturn Sqr(-2*Log(x1)) * Cos(2*#PI*x2)
EndProcedure
 
 
Define i, n=1000
 
Dim a.q(n-1)
For i = 0 To n-1
a(i) = 1 + 0.5 * RandomNormal()
Next</lang>
 
=={{header|Python}}==
;Using random.gauss:
<langsyntaxhighlight lang="python">>>> import random
>>> values = [random.gauss(1, .5) for i in range(1000)]
>>> </langsyntaxhighlight>
 
;Quick check of distribution:
<langsyntaxhighlight lang="python">>>> def quick_check(numbers):
count = len(numbers)
mean = sum(numbers) / count
Line 2,024 ⟶ 2,334:
>>> quick_check(values)
(1.0140373306786599, 0.49943411329234066)
>>> </langsyntaxhighlight>
 
Note that the ''random'' module in the Python standard library supports a number of statistical distribution methods.
 
;Alternatively using random.normalvariate:
<langsyntaxhighlight lang="python">>>> values = [ random.normalvariate(1, 0.5) for i in range(1000)]
>>> quick_check(values)
(0.990099111944864, 0.5029847005836282)
>>> </langsyntaxhighlight>
 
=={{header|R}}==
<syntaxhighlight lang="rsplus"># For reproducibility, set the seed:
<lang r>result <- rnorm(1000, mean=1, sd=0.5)</lang>
set.seed(12345L)
 
result <- rnorm(1000, mean = 1, sd = 0.5)</syntaxhighlight>
 
=={{header|Racket}}==
<syntaxhighlight lang="racket">
<lang Racket>
#lang racket
(for/list ([i 1000])
(add1 (* (sqrt (* -2 (log (random)))) (cos (* 2 pi (random))) 0.5)))
</syntaxhighlight>
</lang>
 
Alternative:
<syntaxhighlight lang="racket">
<lang Racket>
#lang racket
(require math/distributions)
(sample (normal-dist 1.0 0.5) 1000)
</syntaxhighlight>
</lang>
 
=={{header|Raku}}==
Line 2,055 ⟶ 2,368:
{{works with|Rakudo|#22 "Thousand Oaks"}}
 
<syntaxhighlight lang="raku" perl6line>sub randnorm ($mean, $stddev) {
$mean + $stddev * sqrt(-2 * log rand) * cos(2 * pi * rand)
}
Line 2,064 ⟶ 2,377:
say my $mean = @nums R/ [+] @nums;
say my $stddev = sqrt $mean**2 R- @nums R/ [+] @nums X** 2;
</syntaxhighlight>
</lang>
 
=={{header|Raven}}==
<langsyntaxhighlight lang="raven">define PI
-1 acos
Line 2,079 ⟶ 2,392:
2 / 1 +
1000 each drop randNormal "%f\n" print</langsyntaxhighlight>
Quick Check (on linux with code in file rand.rv)
<langsyntaxhighlight lang="raven">raven rand.rv | awk '{sum+=$1; sumsq+=$1*$1;} END {print "stdev = " sqrt(sumsq/NR - (sum/NR)**2); print "mean = " sum/NR}'
stdev = 0.497773
mean = 1.01497</langsyntaxhighlight>
 
=={{header|ReScript}}==
{{trans|OCaml}}
<syntaxhighlight lang="rescript">let pi = 4.0 *. atan(1.0)
 
let random_gaussian = () => {
1.0 +.
sqrt(-2.0 *. log(Random.float(1.0))) *.
cos(2.0 *. pi *. Random.float(1.0))
}
 
let a = Belt.Array.makeBy(1000, (_) => random_gaussian ())
 
for i in 0 to 10 {
Js.log(a[i])
}</syntaxhighlight>
 
=={{header|REXX}}==
Line 2,091 ⟶ 2,420:
Programming note: &nbsp; note the range of the random numbers: &nbsp; (0,1]
<br>(that is, random numbers from &nbsp; zero──►unity, &nbsp; excluding zero, including unity).
<langsyntaxhighlight lang="rexx">/*REXX pgm generates 1,000 normally distributed numbers: mean=1, standard deviation=½.*/
numeric digits 20 /*the default decimal digit precision=9*/
parse arg n seed . /*allow specification of N and the seed*/
Line 2,135 ⟶ 2,464:
m.=9; do j=0 while h>9; m.j=h; h=h%2 + 1; end /*j*/
do k=j+5 to 0 by -1; numeric digits m.k; g=(g+x/g)*.5; end /*k*/
numeric digits d; return g/1</langsyntaxhighlight>
'''output''' &nbsp; when using the default inputs:
<pre>
Line 2,146 ⟶ 2,475:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
for i = 1 to 10
see random(i) + nl
next i
</syntaxhighlight>
</lang>
 
=={{header|RubyRPL}}==
≪ RAND LN NEG 2 * √
<lang ruby>Array.new(1000) { 1 + Math.sqrt(-2 * Math.log(rand)) * Math.cos(2 * Math::PI * rand) }</lang>
RAND 2 * π * COS *
→NUM 2 / 1 +
≫ '<span style="color:blue>RANDN</span>' STO
≪ CL∑
1 1000 '''START''' <span style="color:blue>RANDN</span> ∑+ '''NEXT'''
MEAN PSDEV
≫ '<span style="color:blue>TASK</span>' STO
{{out}}
<pre>
1: .990779804949
2: .487204045227
</pre>
The collection is stored in a predefined array named <code>∑DAT</code>, which is automatically created/updated when using the <code>∑+</code> instruction and remains available until the user decides to purge it, typically by calling the <code>CL∑</code> command.
 
=={{header|Run BASICRuby}}==
<syntaxhighlight lang="ruby">Array.new(1000) { 1 + Math.sqrt(-2 * Math.log(rand)) * Math.cos(2 * Math::PI * rand) }</syntaxhighlight>
<lang runbasic>dim a(1000)
pi = 22/7
for i = 1 to 1000
a( i) = 1 + .5 * (sqr(-2 * log(rnd(0))) * cos(2 * pi * rnd(0)))
next i</lang>
 
=={{header|Rust}}==
{{libheader|rand}}
'''Using a for-loop:'''
<langsyntaxhighlight lang="rust">extern crate rand;
use rand::distributions::{Normal, IndependentSample};
 
Line 2,175 ⟶ 2,514:
*num = normal.ind_sample(&mut rng);
}
}</langsyntaxhighlight>
 
'''Using iterators:'''
<langsyntaxhighlight lang="rust">extern crate rand;
use rand::distributions::{Normal, IndependentSample};
 
Line 2,187 ⟶ 2,526:
(0..1000).map(|_| normal.ind_sample(&mut rng)).collect()
};
}</langsyntaxhighlight>
 
=={{header|SAS}}==
<syntaxhighlight lang="sas">
<lang SAS>
/* Generate 1000 random numbers with mean 1 and standard deviation 0.5.
SAS version 9.2 was used to create this code.*/
Line 2,203 ⟶ 2,542:
end;
run;
</syntaxhighlight>
</lang>
Results:
<pre>
Line 2,217 ⟶ 2,556:
 
=={{header|Sather}}==
<langsyntaxhighlight lang="sather">class MAIN is
main is
a:ARRAY{FLTD} := #(1000);
Line 2,235 ⟶ 2,574:
#OUT + "dev " + dev + "\n";
end;
end;</langsyntaxhighlight>
 
=={{header|Scala}}==
===One liner===
<langsyntaxhighlight lang="scala">List.fill(1000)(1.0 + 0.5 * scala.util.Random.nextGaussian)</langsyntaxhighlight>
===Academic===
<langsyntaxhighlight lang="scala">
object RandomNumbers extends App {
 
Line 2,270 ⟶ 2,609:
println(calcAvgAndStddev(distribution.take(1000))) // e.g. (1.0061433267806525,0.5291834867560893)
}
</syntaxhighlight>
</lang>
 
=={{header|Scheme}}==
<langsyntaxhighlight lang="scheme">; linear congruential generator given in C99 section 7.20.2.1
(define ((c-rand seed)) (set! seed (remainder (+ (* 1103515245 seed) 12345) 2147483648)) (quotient seed 65536))
 
Line 2,319 ⟶ 2,658:
 
(mean-sdev v)
; (0.9562156817697293 0.5097087109575911)</langsyntaxhighlight>
 
=={{header|Seed7}}==
<langsyntaxhighlight lang="seed7">$ include "seed7_05.s7i";
include "float.s7i";
include "math.s7i";
Line 2,346 ⟶ 2,685:
rands[i] := 1.0 + 0.5 * randomNormal;
end for;
end func;</langsyntaxhighlight>
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">var arr = 1000.of { 1 + (0.5 * sqrt(-2 * 1.rand.log) * cos(Num.tau * 1.rand)) }
arr.each { .say }</langsyntaxhighlight>
 
=={{header|Standard ML}}==
Line 2,360 ⟶ 2,699:
You can call the generator with <code>()</code> repeatedly to get a word in the range <code>[Rand.randMin, Rand.randMax]</code>.
You can use the <code>Rand.norm</code> function to transform the output into a <code>real</code> from 0 to 1, or use the <code>Rand.range (i,j)</code> function to transform the output into an <code>int</code> of the given range.
<langsyntaxhighlight lang="sml">val seed = 0w42;
val gen = Rand.mkRandom seed;
fun random_gaussian () =
1.0 + Math.sqrt (~2.0 * Math.ln (Rand.norm (gen ()))) * Math.cos (2.0 * Math.pi * Rand.norm (gen ()));
val a = List.tabulate (1000, fn _ => random_gaussian ());</langsyntaxhighlight>
 
2) Random (a subtract-with-borrow generator). You create the generator by calling <code>Random.rand</code> with a seed (of a pair of <code>int</code>s). You can use the <code>Random.randInt</code> function to generate a random int over its whole range; <code>Random.randNat</code> to generate a non-negative random int; <code>Random.randReal</code> to generate a <code>real</code> between 0 and 1; or <code>Random.randRange (i,j)</code> to generate an <code>int</code> in the given range.
<langsyntaxhighlight lang="sml">val seed = (47,42);
val gen = Random.rand seed;
fun random_gaussian () =
1.0 + Math.sqrt (~2.0 * Math.ln (Random.randReal gen)) * Math.cos (2.0 * Math.pi * Random.randReal gen);
val a = List.tabulate (1000, fn _ => random_gaussian ());</langsyntaxhighlight>
 
Other implementations of Standard ML have their own random number generators. For example, Moscow ML has a <code>Random</code> structure that is different from the one from SML/NJ.
{{works with|PolyMLPoly/ML}}
The SML Basis Library does not provide a routine for uniform deviate generation, and PolyML does not have one. Using a routine from "Monte Carlo" by Fishman (Springer), in the function uniformdeviate, and avoiding the slow IntInf's:
<syntaxhighlight lang="sml">
<lang smlh>
val urandomlist = fn seed => fn n =>
let
Line 2,413 ⟶ 2,752:
val anyrealseed=1009.0 ;
makeNormals bmconv (urandomlist anyrealseed 2000);
</syntaxhighlight>
</lang>
 
=={{header|Stata}}==
<langsyntaxhighlight lang="stata">clear all
set obs 1000
gen x=rnormal(1,0.5)</langsyntaxhighlight>
 
=== Mata ===
<langsyntaxhighlight lang="stata">a = rnormal(1000,1,1,0.5)</langsyntaxhighlight>
 
=={{header|Tcl}}==
<langsyntaxhighlight lang="tcl">package require Tcl 8.5
variable ::pi [expr acos(0)]
proc ::tcl::mathfunc::nrand {} {
Line 2,434 ⟶ 2,773:
for {set i 0} {$i < 1000} {incr i} {
lappend result [expr {$mean + $stddev*nrand()}]
}</langsyntaxhighlight>
 
=={{header|TI-83 BASIC}}==
Builtin function: randNorm()
randNorm(1,.5)
 
Or by a program:
 
Calculator symbol translations:
 
"STO" arrow: &#8594;
 
Square root sign: &#8730;
 
ClrList L<sub>1</sub>
Radian
For(A,1,1000)
√(-2*ln(rand))*cos(2*π*A)→L<sub>1</sub>(A)
End
 
=={{header|TorqueScript}}==
<langsyntaxhighlight lang="tqs">for (%i = 0; %i < 1000; %i++)
%list[%i] = 1 + mSqrt(-2 * mLog(getRandom())) * mCos(2 * $pi * getRandom());</langsyntaxhighlight>
 
=={{header|Ursala}}==
Line 2,468 ⟶ 2,789:
a standard normal distribution. Mean and standard deviation
library functions are also used in this example.
<langsyntaxhighlight Ursalalang="ursala">#import nat
#import flo
 
Line 2,481 ⟶ 2,802:
^(mean,stdev)* <
pop_stats(1.,0.5) 1000,
sample_stats(1.,0.5) 1000></langsyntaxhighlight>
The output shows the mean and standard deviation for both sample vectors,
the latter being exact by construction.
Line 2,489 ⟶ 2,810:
 
=={{header|Visual FoxPro}}==
<langsyntaxhighlight lang="vfp">
LOCAL i As Integer, m As Double, n As Integer, sd As Double
py = PI()
Line 2,516 ⟶ 2,837:
RETURN z
ENDFUNC
</syntaxhighlight>
</lang>
 
=={{header|V (Vlang)}}==
<syntaxhighlight lang="Vlang">
import crypto.rand
 
fn main() {
mut nums := []u64{}
for _ in 0..1000 {
nums << rand.int_u64(10000) or {0} // returns random unsigned 64-bit integer from real OS source of entropy
}
println(nums)
}
</syntaxhighlight>
 
=={{header|Wren}}==
<langsyntaxhighlight ecmascriptlang="wren">import "random" for Random
 
var rand = Random.new()
Line 2,541 ⟶ 2,875:
var mean = sum / n
System.print("Actual mean : %(mean)")
System.print("Actual std dev: %(stdDev.call(numbers, mean))")</langsyntaxhighlight>
 
{{out}}
Line 2,549 ⟶ 2,883:
Actual std dev: 0.4961645117026
</pre>
 
=={{header|XPL0}}==
{{trans|C}}
<syntaxhighlight lang "XPL0">define PI = 3.14159265358979323846;
 
func real DRand; \Uniform distribution, [0..1]
return float(Ran(1_000_000)) / 1e6;
 
func real RandomNormal; \Normal distribution, centered on 0, std dev 1
return sqrt(-2.*Log(DRand)) * Cos(2.*PI*DRand);
 
int I;
real Rands(1000);
for I:= 0 to 1000-1 do
Rands(I):= 1.0 + 0.5*RandomNormal</syntaxhighlight>
 
=={{header|Yorick}}==
Returns array of ''count'' random numbers with mean 0 and standard deviation 1.
<langsyntaxhighlight lang="yorick">func random_normal(count) {
return sqrt(-2*log(random(count))) * cos(2*pi*random(count));
}</langsyntaxhighlight>
 
Example of basic use:
Line 2,571 ⟶ 2,920:
 
=={{header|zkl}}==
<langsyntaxhighlight lang="zkl">fcn mkRand(mean,sd){ //normally distributed random w/mean & standard deviation
pi:=(0.0).pi; // using the Box–Muller transform
rz1:=fcn{1.0-(0.0).random(1)} // from [0,1) to (0,1]
return('wrap(){((-2.0*rz1().log()).sqrt() * (2.0*pi*rz1()).cos())*sd + mean })
}</langsyntaxhighlight>
This creates a new random number generator, now to use it:
<langsyntaxhighlight lang="zkl">var g=mkRand(1,0.5);
ns:=(0).pump(1000,List,g); // 1000 rands with mean==1 & sd==1/2
mean:=(ns.sum(0.0)/1000); //-->1.00379
// calc sd of list of numbers:
(ns.reduce('wrap(p,n){p+(n-mean).pow(2)},0.0)/1000).sqrt() //-->0.494844</langsyntaxhighlight>
 
=={{header|ZX Spectrum Basic}}==
 
Here we have converted the QBasic code to suit the ZX Spectrum:
 
<lang zxbasic>10 RANDOMIZE 0 : REM seeds random number generator based on uptime
20 DIM a(1000)
30 CLS
40 FOR i = 1 TO 1000
50 LET a(i) = 1 + SQR(-2 * LN(RND)) * COS(2 * PI * RND)
60 NEXT i</lang>
512

edits