# 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

## 4 thoughts on “Estimating Pi by Monte Carlo using mXparser”

1. Kenny says:

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. Mariusz Gromada says:

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

2. George Canepa says:

Mariuz

Great library! I was working on something like this, found yours yesterday, and already integrated into my app. It is way beyond what I could ever do.

I am seeing something odd with the solve capability. I may be missing something, but read as much documentation as I could find

When I do a “solve( x^2+7.5*x-70.2, x, 100, -100)”.calculate() sometimes I get 5.429 and other times I get -12.929 which are both valid.

Is there a way to get all the roots in the answer consistently (or even consistent behavior, like the 1st root in the range, I could then adjust the range to find the next root, and so on)

George Canepa

1. Mariusz Gromada says:

Dear George,