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.*/
!=0; @reps='repetitions: Monte Carlo pi is' /*pi decimal digit accuracy (so far).*/
accur=0 /*accuracy of Monte Carlo pi (so far). */
accur=0 /*accuracy of Monte Carlo pi (so far). */
!=0; @reps='repetitions: Monte Carlo pi is' /*pi decimal digit accuracy (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 /*compute the number of repetitions. */
reps=chunk*j /*calculate the number of repetitions. */
piX=4*! / reps /*let's see how this puppy does so far.*/
_=compare(4*! / reps, pi) /*compare apples and ··· crabapples. */
_=compare(piX, 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. */