Home > database >  How can I summarize the probability results, that I got with a loop? (Java)
How can I summarize the probability results, that I got with a loop? (Java)

Time:11-22

I'm wondering if is there a way to summarize the results of probability that I got through a loop? So I can know how many successful hits there were, given x number of attempts. Right now I just get a stream of 1 or 0 statements (1 for a successful hit, 0 for fail), not very practical. It looks like this:

public class doGry {
public static void main(String[] args ) {
    for (int i = 0; i < 50; i  ) {
        // chance to hit (h) = 35%   (ma - md)
        // 8% < h < 90%
        double ma = 20;
        double md = 10;
        double probability;

        System.out.println("probability of success "   (probability = 35   (ma - md)));

        double probab2 = probability / 100;
        double r = Math.random();

        int roll;
        if (r <= probab2) roll = 1;
        else roll = 0;
        System.out.println(roll);


    }

}

}

CodePudding user response:

There are few variables that can be initialized outside of for-loop since you are not modifying its value, so no need it initialize it again and again.
Using a variable count, for keeping track of successful hits. For each successful hit, increment its value by 1.

public class doGry {
    public static void main(String[] args ) {
        double ma = 20;
        double md = 10;
        double probability = 35   (ma - md);
        double probab2 = probability / 100;
        int count = 0;
        for (int i = 0; i < 50; i  ) {

            System.out.println("probability of success "   probability);
            
            double r = Math.random();

            if (r <= probab2) 
            {
                System.out.println("1");
                count  ;
            }
            else 
                System.out.println("0");
        }
        System.out.println("Succesful hits "   count);
    }
}

CodePudding user response:

All you have to do is add in a variable to keep track of how many successful rolls occurred, and then divide that by the total number of rolls using a double like so

public class doGry
{
    public static void main(String[] args )
    {
        int total = 0;
        for (int i = 0; i < 50; i  )
        {
            // chance to hit (h) = 35%   (ma - md)
            // 8% < h < 90%
            double ma = 20.0;
            double md = 10.0;
            double probability;
    
            System.out.println("probability of success "   (probability = 35   (ma - md)));
    
            double probab2 = probability / 100;
            double r = Math.random();
    
            int roll;
            if (r <= probab2) {roll = 1; total  ;}
            else roll = 0;
            System.out.println(roll);
        }
        System.out.println("total: "   total/50.0);
    }
}

Notice in the final System.out.println statement, the total is being divided by 50.0 (and not 50) as the .0 indicates you want a double division and not integer division, the latter of which throws away any remainder decimal values.

  • Related