Chance That Two Random Draws Multiply to More Than a Half
Math & Bit ManipulationAuto-graded · 18 test casesLast updated
Compute the exact probability that the product of two independent Uniform(0,1) values exceeds 1/2, by integrating over the region of the unit square above a hyperbola.
Two numbers U and V are drawn independently, each uniformly at random from the open interval (0, 1). Consider the event that their product is larger than one half, i.e. U * V > 1/2.
Write a function that returns the probability of this event as a floating-point number.
The function takes no arguments. Your result should be the exact value of the probability, evaluated in floating point, and not an estimate obtained from random simulation.
Notes:
- The pair
(U, V)is uniformly distributed over the unit square, so the probability equals the area of the part of the square whereU * V > 1/2. - Whether the inequality is strict or not makes no difference, because the boundary curve
U * V = 1/2has zero area.
Constraints
UandVare independent and continuously uniform on(0, 1).- The function has no parameters and returns a single
float. - Return the closed-form value computed with standard floating-point math (no Monte Carlo sampling).
- Answers are compared to the expected value with a tiny numerical tolerance.
Examples
Example 1
Input: prob_product_exceeds_half()
Output: 0.15342640972002736
Example 2
Input: prob_product_exceeds_half()
Output: 0.15342640972002736
Hints
Hint 1
Because the pair is uniform on the unit square, the probability is just an area. Describe the region where u * v > 1/2.
Hint 2
For a fixed u, the condition becomes v > 1/(2u). When is this threshold below 1? When u <= 1/2 no valid v exists, so only u in (1/2, 1) contributes.
Hint 3
For u in (1/2, 1) the valid v-range has length 1 - 1/(2u). Integrate this over u from 1/2 to 1 and use the antiderivative of 1/u, which is ln u.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The point (U, V) is uniform on the unit square, so the desired probability is the area of the region {(u, v) : u * v > 1/2} inside the square.
Fix u and ask which v values work. The condition u * v > 1/2 is equivalent to v > 1 / (2u).
- If
u <= 1/2, then1/(2u) >= 1, which is outside the interval forv. Novworks, so the contribution is zero. - If
1/2 < u < 1, then the threshold1/(2u)lies in(1/2, 1), and the set of validvhas length1 - 1/(2u).
Integrating the conditional probability over u:
P = ∫ from 1/2 to 1 of (1 - 1/(2u)) du
= [ u - (1/2) ln u ] evaluated from 1/2 to 1
= 1 - (1/2 - (1/2) ln(1/2))
= (1 - ln 2) / 2
≈ 0.15343
Why it is correct
Independence and uniformity make the joint density equal to 1 on the unit square, so area equals probability. Restricting the integral to u > 1/2 avoids counting values of u for which the event is impossible. A sanity check: the region lies inside the square (1/2, 1) x (1/2, 1) of area 1/4, and 0.153 is below 0.25, as it must be.
Complexity
Time O(1) and space O(1), since the answer is a closed-form expression.
Reference solution
import math
def prob_product_exceeds_half() -> float:
# P = integral from 1/2 to 1 of (1 - 1/(2u)) du = (1 - ln 2) / 2
return (1.0 - math.log(2.0)) / 2.0