Skip to content

Instantly share code, notes, and snippets.

@twizmwazin
Created December 29, 2015 03:15
Show Gist options
  • Select an option

  • Save twizmwazin/99b40614a3957dfdb954 to your computer and use it in GitHub Desktop.

Select an option

Save twizmwazin/99b40614a3957dfdb954 to your computer and use it in GitHub Desktop.
import java.lang.Integer;
import java.util.Random;
public class PiGenorator {
public static void main(String[] args) {
try {
int seed = Integer.parseInt(args[0]);
int trials = Integer.parseInt(args[1]);
Random random = new Random(seed);
int total = 0;
for (int i = 0; i < trials; i++) {
if (Math.sqrt(Math.pow(random.nextDouble(), 2.0) + Math.pow(random.nextDouble(), 2.0)) <= 1) total++;
}
double pi = ((double) total / (double) trials) * 4.0;
System.out.println("Pi approximated to " + pi + "");
} catch (Exception e) {
System.out.println("args: seed, trials");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment