Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Montecarlo {
- private static int FALLIDO = 0;
- private static int SIN_FALLA = 1;
- private static int MAX_TIRADAS = 9999999;
- private static double EPSILON = 0.000001;
- private int fallidos = 5;
- private int sin_fallas = 5;
- private int totales = 10;
- public Montecarlo() {}
- public double getProbability(){
- int exitos = 0;
- int tiradas = 0;
- double prob_ant = -1;
- double prob_act = 0;
- while(!converge(prob_ant,prob_act,tiradas)) {
- int p1 = getProduct();
- int p2 = getProduct();
- int p3 = getProduct();
- if ( (p1 == FALLIDO) && (p2 == FALLIDO ) && (p3 == FALLIDO) )
- exitos ++;
- tiradas ++;
- prob_ant = prob_act;
- prob_act = (double) exitos/tiradas;
- resetValues();
- }
- return prob_act;
- }
- private void resetValues() {
- fallidos = 5;
- sin_fallas = 5;
- totales = 10;
- }
- private int getProduct(){
- double fallas =(double) fallidos / totales;
- double[] prob_acum = {fallas,1.0F};
- double r = Math.random();
- for(int i = 0; i < prob_acum.length;i++) {
- if (r < prob_acum[i] && i == FALLIDO ) {
- totales --;
- fallidos--;
- return i;
- }
- else if(r < prob_acum[i] && i == SIN_FALLA ) {
- totales --;
- sin_fallas--;
- return i;
- }
- }
- return -1;
- }
- boolean converge( double ant, double act, int tiradas) {
- if((Math.abs(ant- act) < EPSILON) && tiradas > MAX_TIRADAS)
- return true;
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement