t0 = clock; N = 10000000; count = 0; radius = 1; disp(N); for k = 1:N % Generate two numbers between 0 and 1 % Here, p(x,y) represents a point in the x-y space p = rand(1,2); % Check whether the point p(x,y) lies inside the circle of unit radius % i.e. test for the condition : x^2 + y^2 < 1 if sum(p.^2) < radius % Point is inside circle : Increment count count = count + 1; end end pival = 4*count/N; t1 = clock; fprintf('Calculated PI = %f\nError = %f\n', pival, pi-pival); fprintf('Total time : %f seconds\n', etime(t1, t0));