-
-
Notifications
You must be signed in to change notification settings - Fork 357
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
101 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,3 +13,4 @@ Chinmaya Mahesh | |
Unlambder | ||
Kjetil Johannessen | ||
CDsigma | ||
DominikRafacz |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
//submitted by DominikRafacz | ||
import java.util.Random; | ||
|
||
public class MonteCarlo { | ||
|
||
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; | ||
} | ||
|
||
//function to calculate estimation of pi | ||
public static void monteCarlo(int samples) { | ||
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)) { | ||
piCount++; | ||
} | ||
} | ||
|
||
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters