Monte Carlo methods: Difference between revisions
Content added Content deleted
(→{{header|REXX}}: added a scale to show the number of (true) decimal digits of pi; added expanded output.) |
m (→{{header|REXX}}: optimized the outer DO loop.) |
||
Line 1,521: | Line 1,521: | ||
limit=10000-1 /*REXX random generates only integers. */ |
limit=10000-1 /*REXX random generates only integers. */ |
||
limitSq=limit**2 /*··· so, instead of one, use limit**2.*/ |
limitSq=limit**2 /*··· so, instead of one, use limit**2.*/ |
||
⚫ | |||
accur=0 /*accuracy of Monte Carlo pi (so far). */ |
accur=0 /*accuracy of Monte Carlo pi (so far). */ |
||
⚫ | |||
say /*a blank line, just for the eyeballs.*/ |
say /*a blank line, just for the eyeballs.*/ |
||
do j=1 for times%chunk |
do j=1 for times%chunk |
||
Line 1,528: | Line 1,528: | ||
if random(0,limit)**2 + random(0,limit)**2 <=limitSq then !=!+1 |
if random(0,limit)**2 + random(0,limit)**2 <=limitSq then !=!+1 |
||
end /*chunk*/ |
end /*chunk*/ |
||
reps=chunk*j /* |
reps=chunk*j /*calculate the number of repetitions. */ |
||
_=compare(4*! / reps, pi) /*compare apples and ··· crabapples. */ |
|||
_= |
if _<=accur then iterate /*if not better accuracy, keep trukin'.*/ |
||
if _<=accur then iterate /*if not better accuracy, keep going. */ |
|||
say right(commas(reps),20) @reps 'accurate to' _-1 "places." /*-1 for dec. pt.*/ |
say right(commas(reps),20) @reps 'accurate to' _-1 "places." /*-1 for dec. pt.*/ |
||
accur=_ /*use this accuracy for next baseline. */ |
accur=_ /*use this accuracy for next baseline. */ |