Skip to content

Instantly share code, notes, and snippets.

@Ranzeplay
Created January 9, 2024 14:58
Show Gist options
  • Select an option

  • Save Ranzeplay/c4b9ad2485626fc1acf0abd96920371f to your computer and use it in GitHub Desktop.

Select an option

Save Ranzeplay/c4b9ad2485626fc1acf0abd96920371f to your computer and use it in GitHub Desktop.
import java.util.Random;
public class Main {
public static void main(String[] args) {
for(var a = 0; a < 100; a++) {
int count = 0;
for (var i = 0; i < 20000; i++) {
var x = new Random().doubles(0, 2).findFirst().getAsDouble();
var y = new Random().doubles(0, 2).findFirst().getAsDouble();
if ((x - 1) * (x - 1) + (y - 1) * (y - 1) <= 1) {
count++;
}
}
System.out.println("Approx PI value: " + 4 * count / (double)20000);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment