3
$\begingroup$

We're doing a simple exercise and we have the following situation:

if ( random(1-100) == 50 OR random( 1-100 ) == 50 ) THEN [..]

The random function may return any number between 1-100 1 and 100 included with a theoretic complete randomization.

I'd like to know what is the % of that IF statement to return TRUE.

another question is:

Considering the OR statement aqs short-circuit does it affect in any way that %.

$\endgroup$
1
  • $\begingroup$ What does "aqs short-circuit" mean? I can't follow that part of the question $\endgroup$
    – Glen_b
    Commented Sep 1, 2016 at 16:05

2 Answers 2

4
$\begingroup$

Let X = random(1-100) and Y = random( 1-100 ). That is uniformly distributed between the integers $1, 2, 3 \dots 100$

Then $P(X=50 \cup Y=50) = P(X=50)+P(Y=50)-P(X=50 \cap Y=50)$.

If the variables are independent then the probability should be: $0.01+0.01-0.01^2 = 0.0199 $. So, unless I misunderstand your statement, it is true almost 2% of the time.

Also assuming your function can only take integer values (which occur with equal probability), otherwise (if the function can take any real number in the interval) the probability should be 0.

$\endgroup$
1
  • $\begingroup$ Simulate in R with n <- 1e5; a <- sample(1:100, size = n, replace = TRUE); b <- sample(1:100, size = n, replace = TRUE); sum(b == 50 | a == 50)/n $\endgroup$
    – snoram
    Commented Sep 1, 2016 at 15:36
2
$\begingroup$

You can model this as a binomial probability.

$X \sim Binom(n=2, p = 1/100)$

$P(X\geq 1) = 0.0199$

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.