Skip to content

Instantly share code, notes, and snippets.

@FlorianCassayre
Created March 5, 2017 00:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FlorianCassayre/bd36cc193656e3e353cc000fef5de4ea to your computer and use it in GitHub Desktop.
Save FlorianCassayre/bd36cc193656e3e353cc000fef5de4ea to your computer and use it in GitHub Desktop.
Probably one of the least efficient way to approximate e.
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