Created
March 5, 2017 00:50
-
-
Save FlorianCassayre/bd36cc193656e3e353cc000fef5de4ea to your computer and use it in GitHub Desktop.
Probably one of the least efficient way to approximate e.
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
import java.util.Random; | |
public class MonteCarloEuler | |
{ | |
private static final Random random = new Random(); | |
public static void main(String[] args) | |
{ | |
final int total = 100000; | |
final int n = 10000; | |
int successes = 0; | |
for(int i = 0; i < total; i++) | |
if(!binomialTrial(n)) | |
successes++; | |
System.out.println((double) total / successes); | |
} | |
private static boolean binomialTrial(int n) | |
{ | |
for(int i = 0; i < n; i++) | |
if(random.nextInt(n) == 0) | |
return true; | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment