Check out example codes for "calculate pi in java". It will help you in understanding the concepts better.

Code Example 1

private static double calcPi(final int iterations){
        double x;
        double y;
        int successCount = 0;
        for (int i = 0; i <= iterations; i++){
            x = Math.random();
            y = Math.random();
            if ((Math.pow(x, 2) + Math.pow(y, 2)) <= 1){
                successCount++;
            }
        }
	    return (double) (4 * successCount) / iterations;
    }

Learn ReactJs, React Native from akashmittal.com