From 3bd9d0dd082b5ea525b279d85a811238d62cf073 Mon Sep 17 00:00:00 2001 From: DominikRafacz Date: Sat, 30 Jun 2018 22:07:04 +0200 Subject: [PATCH 1/4] Added monte carlo method in Java --- CONTRIBUTORS.md | 3 +- .../monte_carlo/code/java/MonteCarlo.java | 34 +++++++++++++++++++ chapters/monte_carlo/monte_carlo.md | 5 +++ 3 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 chapters/monte_carlo/code/java/MonteCarlo.java diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index f60110b82..fbf31db5f 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -11,4 +11,5 @@ Pen Pal Chinmaya Mahesh Unlambder Kjetil Johannessen -CDsigma \ No newline at end of file +CDsigma +DominikRafacz diff --git a/chapters/monte_carlo/code/java/MonteCarlo.java b/chapters/monte_carlo/code/java/MonteCarlo.java new file mode 100644 index 000000000..28ca91ba8 --- /dev/null +++ b/chapters/monte_carlo/code/java/MonteCarlo.java @@ -0,0 +1,34 @@ +//submitted by DominikRafacz +import java.util.Random; + +public class MonteCarlo { + + public static void main(String[] args){ + monteCarlo(10_000_000, 0.5); + } + + //function to check whether point (x,y) is in circle of radius r + private static boolean inCircle(double x, double y, double radius){ + return x*x + y*y < radius*radius; + } + + //function to calculate estimation of pi + public static void monteCarlo(int samples, double radius){ + int piCount = 0; + + Random random = new Random(); + + for(int i = 0; i < samples; i++){ + double x = random.nextDouble(); + double y = random.nextDouble(); + if(inCircle(x, y, radius)){ + piCount++; + } + } + + double estimation = 4 * piCount / (samples * radius * radius); + + System.out.println("Estimated pi value: " + estimation); + System.out.printf("Percent error: %.4f%%", 100*(Math.PI-estimation)/Math.PI); + } +} diff --git a/chapters/monte_carlo/monte_carlo.md b/chapters/monte_carlo/monte_carlo.md index c312370df..547ce8902 100644 --- a/chapters/monte_carlo/monte_carlo.md +++ b/chapters/monte_carlo/monte_carlo.md @@ -49,6 +49,8 @@ each point is tested to see whether it's in the circle or not: [import:2-5, lang:"d"](code/d/monte_carlo.d) {% sample lang="go" %} [import:12-14, lang:"golang"](code/go/monteCarlo.go) +{% sample lang="java" %} +[import:11-12, lang:"java"](code/java/MonteCarlo.java) {% endmethod %} If it's in the circle, we increase an internal count by one, and in the end, @@ -98,6 +100,9 @@ Feel free to submit your version via pull request, and thanks for reading! {%sample lang="go" %} ### Go [import, lang:"golang"](code/go/monteCarlo.go) +{% sample lang="java" %} +### Java +[import, lang:"java"](code/java/MonteCarlo.java) {% endmethod %} From 9236c4637b614c9b7e54439cde45a25a31ba6d17 Mon Sep 17 00:00:00 2001 From: DominikRafacz Date: Sat, 30 Jun 2018 22:20:52 +0200 Subject: [PATCH 2/4] fixed number of lines in md file --- chapters/monte_carlo/monte_carlo.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/chapters/monte_carlo/monte_carlo.md b/chapters/monte_carlo/monte_carlo.md index 547ce8902..dbb350fa3 100644 --- a/chapters/monte_carlo/monte_carlo.md +++ b/chapters/monte_carlo/monte_carlo.md @@ -50,7 +50,7 @@ each point is tested to see whether it's in the circle or not: {% sample lang="go" %} [import:12-14, lang:"golang"](code/go/monteCarlo.go) {% sample lang="java" %} -[import:11-12, lang:"java"](code/java/MonteCarlo.java) +[import:11-13, lang:"java"](code/java/MonteCarlo.java) {% endmethod %} If it's in the circle, we increase an internal count by one, and in the end, From 16b5ae9eb820a9bfcfc224abe67ea20044c84020 Mon Sep 17 00:00:00 2001 From: DominikRafacz Date: Sun, 1 Jul 2018 16:59:18 +0200 Subject: [PATCH 3/4] Set fixed radius to 1 --- chapters/monte_carlo/code/java/MonteCarlo.java | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/chapters/monte_carlo/code/java/MonteCarlo.java b/chapters/monte_carlo/code/java/MonteCarlo.java index 28ca91ba8..00b20ff5d 100644 --- a/chapters/monte_carlo/code/java/MonteCarlo.java +++ b/chapters/monte_carlo/code/java/MonteCarlo.java @@ -4,16 +4,16 @@ public class MonteCarlo { public static void main(String[] args){ - monteCarlo(10_000_000, 0.5); + monteCarlo(10_000_000); } - //function to check whether point (x,y) is in circle of radius r - private static boolean inCircle(double x, double y, double radius){ - return x*x + y*y < radius*radius; + //function to check whether point (x,y) is in unit circle + private static boolean inCircle(double x, double y){ + return x*x + y*y < 1; } //function to calculate estimation of pi - public static void monteCarlo(int samples, double radius){ + public static void monteCarlo(int samples){ int piCount = 0; Random random = new Random(); @@ -21,12 +21,12 @@ public static void monteCarlo(int samples, double radius){ for(int i = 0; i < samples; i++){ double x = random.nextDouble(); double y = random.nextDouble(); - if(inCircle(x, y, radius)){ + if(inCircle(x, y)){ piCount++; } } - double estimation = 4 * piCount / (samples * radius * radius); + double estimation = 4.0 * piCount / (samples); System.out.println("Estimated pi value: " + estimation); System.out.printf("Percent error: %.4f%%", 100*(Math.PI-estimation)/Math.PI); From 0655f8f55e81e530c052039f90056af28d2b5587 Mon Sep 17 00:00:00 2001 From: DominikRafacz Date: Sun, 1 Jul 2018 22:16:50 +0200 Subject: [PATCH 4/4] Corrected code style --- chapters/monte_carlo/code/java/MonteCarlo.java | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/chapters/monte_carlo/code/java/MonteCarlo.java b/chapters/monte_carlo/code/java/MonteCarlo.java index 00b20ff5d..1c368932c 100644 --- a/chapters/monte_carlo/code/java/MonteCarlo.java +++ b/chapters/monte_carlo/code/java/MonteCarlo.java @@ -3,32 +3,33 @@ public class MonteCarlo { - public static void main(String[] args){ + public static void main(String[] args) { monteCarlo(10_000_000); } //function to check whether point (x,y) is in unit circle - private static boolean inCircle(double x, double y){ - return x*x + y*y < 1; + private static boolean inCircle(double x, double y) { + return x * x + y * y < 1; } //function to calculate estimation of pi - public static void monteCarlo(int samples){ + public static void monteCarlo(int samples) { int piCount = 0; Random random = new Random(); - for(int i = 0; i < samples; i++){ + for (int i = 0; i < samples; i++) { double x = random.nextDouble(); double y = random.nextDouble(); - if(inCircle(x, y)){ + if (inCircle(x, y)) { piCount++; } } - double estimation = 4.0 * piCount / (samples); + double estimation = 4.0 * piCount / samples; System.out.println("Estimated pi value: " + estimation); - System.out.printf("Percent error: %.4f%%", 100*(Math.PI-estimation)/Math.PI); + System.out.printf("Percent error: %.4f%%", + 100 * (Math.PI - estimation) / Math.PI); } }