Estimating Pi by Monte Carlo using mXparser

Using built-in random variable [Uni] from uniform continuous distribution over [0; 1] interval we sample two numbers (x,y) then check if point is inside the circle. Operation is repeated n-times, then proportion falling into circle is being measured. This is only 1/4 of the circle (positive x and positive y) with radius 1, so 4 times proportion should give estimation of pi.

Argument n = new Argument("n = 100000");
Expression e = new Expression("4 * sum(i, 1, n, if( [Uni]^2 + [Uni]^2 <= 1; 1; 0) ) / n", n);
mXparser.consolePrintln("Res. : " + e.getExpressionString() + " = " + e.calculate());
[mXparser-v.3.0.0] Res. : 4 * sum(i, 1, n, if( [Uni]^2 + [Uni]^2 <= 1; 1; 0) ) / n = 3.14748

Best regards

2 thoughts on “Estimating Pi by Monte Carlo using mXparser

  1. I have implementing MathParser on my labtop which is running on SSD. The duration for the evaluation take more time than using the normal harddisk. If the formula is complex, it could take more than 10 mins to get the result. However normal harddisk just return the result in less than 1 min. Any idea? It looks we can't use SSD for math parser.

    1. Hello,

      Please use the newest version - you can already clone from GitHub. v.4.0 will be released soon. For .net there were few changes related to throwing exceptions - this affected performance in debug mode.

      Best regards

Leave a Reply

Your email address will not be published. Required fields are marked *