What is the probability or percentage or frequency by which hello line will be printed?

66 Views Asked by At

I have a below method which is called every one minute from background thread and that background thread keeps running always.

public void testMethod() {
    // some other code      
    int value = new Random().nextInt(20 - 1 + 1) + 1;
    if(value > 19) {
        // this will print hello line
        System.out.println("hello");        
    }       
    // some other code
}

Now as you can see I am printing hello if my value is greater than 19. My question is how should I say that hello line will be printed at this rate or it will printed this percentage of time so that people can understand this.

If somebody asks me at what frequency or rate hello line will be printed given my above method is called every 1 minute. What should I say?

This line int value = new Random().nextInt(20 - 1 + 1) + 1; will generate random number between $1$ and $20$ inclusive both.

2

There are 2 best solutions below

4
On BEST ANSWER

In every minute the probability that it will print hello is $\frac{1}{20}$ assuming that the "Random" is fair in JAVA

Let $A$ denote the event that it will print hello per one minute so $\mathrm {P}[A]=\frac{|A|}{|\Omega|}=\frac{1}{20}$

Therfore the rate is one hello per $20$ minutes

4
On

Isn't the frequency just once per 20 minutes? "Hello" is printed only when the number is 20. That is with 1/20 probability. So, even if the rate varies each time, over an average of 7 hours, by 5% error margin, you can say the rate is 1 "hello" per 20 minutes.