Jump to content

Monte Carlo methods: Difference between revisions

m
→‎{{header|REXX}}: added/changed whitespace and comments.
m (→‎{{header|REXX}}: added/changed whitespace and comments.)
Line 2,119:
A specific-purpose commatizer function is included to format the number of iterations.
<lang rexx>/*REXX program computes and displays the value of pi÷4 using the Monte Carlo algorithm*/
numeric digits accuracy=_ 20 /*use this20 accuracydecimal fordigits nextto baselinehandle args. */
/*true pi*/ pi=3.141592653589793238462643383279502884197169399375105820974944592307816406
parse arg times chunk digs r? . /*does user want a specific number? */
if times=='' | times=="," then times=5e12 5e12 /*five trillion should do it, hopefully*/
if chunk=='' | chunk=="," then chunk= 100000 /*perform Monte Carlo in 100k chunks.*/
if digs =='' | digs=="," then digs= 99 /*indicates to use length of PI - 1. */
if datatype(r?, 'W') then call random ,,r? /*Is there a random seed? Then use it.*/
exit /*stick a[↓] fork inpi it,meant to we'reline─up allwith done.a SAY.*/
/*true pi*/ pi= 3.141592653589793238462643383279502884197169399375105820974944592307816406
pi= strip( left(pi, digs + length(.) ) ) /*obtain length of pi to what's wanted.*/
numeric digits length(pi) - 1 /*define decimal digits as length PI -1*/
say ' 1 2 3 4 5 6 7 '
say 'scale: 1·234567890123456789012345678901234567890123456789012345678901234567890123'
say /* [↑] a two-linetwo─line scale for showing pi*/
say 'true pi= ' pi"+" /*we might as well brag about true pi.*/
numericsay digits length(pi) - 1 /*thisdisplay programa usesblank theseline decimalfor digsseparation. */
parselimit arg times chunk= .10000 - 1 /*does user want a specific number? /*REXX random generates only integers. */
accuracylimitSq =0 limit **2 /*accuracy··· ofso, Monteinstead Carloof pione, use (so far)limit**2.*/
if times=='' | times=="," then times=5e12 /*five trillion should do it, hopefully*/
if chunk=accuracy='' |0 chunk=="." then chunk=100000 /*performaccuracy of Monte Carlo in pi 100k (so chunksfar).*/
!=0; @reps= 'repetitions: Monte Carlo pi is' /*pi a decimalhandy─dandy digitshort accuracyliteral for (soa far).SAY*/
limit=10000 - 1 /*REXX random generates only integers. */
limitSq!=limit**2 0 /*···!: so, insteadis the accuracy of one,pi use limit**2(so far). */
do j=1 for times % chunk
accuracy=0 /*accuracy of Monte Carlo pi (so far).*/
reps=chunk * j do chunk /*calculatedo theMonte numberCarlo, ofone chunk repetitionsat─a─time. */
!=0; @reps= 'repetitions: Monte Carlo pi is' /*pi decimal digit accuracy (so far).*/
say if random(, limit)**2 + random(, limit)**2 <= limitSq then != ! + /*a blank line, just for the eyeballs.*/1
do j=1 for times % end /*chunk*/
reps= chunk * j do chunk /*docalculate Montethe Carlo,number of onerepetitions. chunk at-a-time.*/
_= compare(4*! / reps, pi) if random(, limit)**2 + random(, limit)/**2compare <=apples limitSqand then··· !=! +crabapples. 1*/
if _<=accuracy then iterate end /*chunkNot better accuracy? Keep truckin'. */
say right(commacommas(reps), 20) @reps 'accurate to' _-1 "places." /*─1 ≡ dec─1≡dec. point*/
reps=chunk * j /*calculate the number of repetitions. */
_ accuracy=compare(4*! /_ reps, pi) /*compare applesuse andthis accuracy ···for next crabapplesbaseline. */
end /*j*/
if _<=accuracy then iterate /*Not better accuracy? Keep truckin'. */
limit=10000exit 0 - 1 /*REXXstick a fork in it, random generateswe're onlyall integersdone. */
say right(comma(reps), 20) @reps 'accurate to' _-1 "places." /*─1 ≡ dec. point*/
accuracy=_ /*use this accuracy for next baseline. */
end /*j*/
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
commacommas: procedure; arg _; do k=length(_)-3 to 1 by -3; _=insert(',',_,k); end; return _</lang>
{{out|output|text=&nbsp; when using the default input:}}
<pre>
Cookies help us deliver our services. By using our services, you agree to our use of cookies.