# Probability formula, a value chosen at random is greater than another chosen value.

Say I have two number ranges, whole numbers only.

Range 1: [-3,16]

Range 2: [3,22]

I choose randomly one number from Range 1 and one number from Range 2.

Lets call them x and y.

How do I find the probability that the number x will be greater than the number y?

$$P(x > y) = P(x,y \in [3,16])[1/2 *P(no\:\: tie \: | \: x,y \in [3,16])]$$

$$= (14/20)(14/20)(1/2 * 13/14)$$

$$= 91/400.$$

X = -3:16
Y = 3:22
sims = 0
count = 0
while(1) {
x = sample(X,1)
y = sample(Y,1)
if (x > y) count = count + 1
sims = sims + 1
}
p = count/sims
error = 3.29*sqrt(p*(1-p)/sims)
p
p-error
p+error
91/400
sims


Output:

> p
[1] 0.2276158
> p-error
[1] 0.2271114
> p+error
[1] 0.2281201
> 91/400
[1] 0.2275
> sims
[1] 7480549


Shown is the 99.9% confidence interval. This runs until you break it, and you can restart it from the main while loop to have it continue until the confidence interval is as tight as you want.

• I'm wondering how can a student understand a numerical story about a resolution method, without rigorous arguments. – xecafe Jun 11 '15 at 8:29
• My argument was that when both players have the same range, each will win half of the non-ties. The student understands this by recognizing an obvious symmetry. My P(no ties) was conditional on having the same range which I didn't make clear originally, so I have edited this. The simulation was intended to be tool the student can use to check the answer to this and other questions, not to be the sole basis for resolution. – BruceZ Jun 11 '15 at 14:10
1. The sample space is $20\cdot20$ = 400

2. Chances of x > y are only in the interval [3,16]

3. In the $14\cdot14$ = 196 sample points, there will be 14 ties,

and by symmetry, for $\frac12$ of the rest , x > y, hence ans = $\frac{91}{400}$

• I wish I could mark two answers as correct. I feel that the current chosen answer is what I was looking for, but your answer explains the process of that chosen answer. – StewVanB Jun 10 '15 at 15:29
• No sweat, your satisfaction is what is important ! – true blue anil Jun 10 '15 at 15:36

One way you could do this is the following:

Let $X$ be the number from the first range, and let $Y$ be the number from the second.

$$\mathbb{P}(X>Y)=\sum_{y\in \{3;22\}}\mathbb{P}(X>Y \mid Y=y)\mathbb{P}(Y=y)$$

The probability that $X>Y$ given that $Y=y$ is easily determined by counting what fraction of the possible values of $X$ are larger than $y$. This type of computation is easily implemented on a computer. By hand, it could be pretty tedious. In general of course you'd replace the sum $y\in \{3;22\}$ with $y\in \text{Range 2}$, i.e. $y$ is over all the possible values of $Y$.

If you write out what those conditional probabilities are you'll notice a pattern in how the values change from one term in the sum to the next. This may lead you (with a little effort) to an easier-to-compute closed form...

Let us denote by R the rectangular region $[-3,16]\times[3,22]$. The required probability is $P(X>Y)=P((X,Y)\in D)$, where $D=\{(x,y)\in R, x>y\}$. A short computation illustrates that D is the triangle of vertices (3,3), (16,3), (16,16), having the area equal to $13^2/2$. Now we have $P(X>Y)=P((X,Y)\in D)=\dfrac{area(D)}{area(R)}=\dfrac{13^2}{2\cdot 19^2}$.

Here is the Python code simulating $(X,Y)$:

import numpy as np
k=0
nr=0
N=5000
for k in range(N):
x=-3+19*np.random.random()
y=3+19*np.random.random()
if x>y:
nr+=1

print 'Experimental prob=', float(nr)/N,  'Theoretical prob=', 13.0**2/(2*19**2),\
'\nBruceZ probability=', float(91)/400

>>> Experimental prob= 0.231 Theoretical prob= 0.234072022161
BruceZ probability= 0.2275

• Reread the question please. The correct answer is 91/400 as I posted. – BruceZ Jun 11 '15 at 13:36
• We can still make use of your picture for our problem where the numbers are integers to produce a result in agreement with mine. Simply count the points that fall in or on the triangle but not on the hypotenuse since those are ties, and divide by the points in or on the square. For the triangle we get $13+12+11+\dots + 1={14 \choose 2}=91$ and for the square we get $20^2=400$, for a probability of $91/400$. – BruceZ Jun 11 '15 at 19:41
• Unfortunately, being used with the name of integer number, not whole number (is this a math slang?!!!!), I didn't realize that the question was formulated for discrete uniform random variables, not for continuous one. – xecafe Jun 12 '15 at 12:31
• According to references in Wikipedia including Wolfram's Mathworld, the term "whole numbers" can refer to all integers, only non-negative integers, or only positive integers. I was always taught it meant non-negative integers. I just assumed he meant integers due to the way the range was specified. In any case, it never refers to non-integers. The OP should be able to make the correct adjustment if something else was intended. – BruceZ Jun 12 '15 at 19:44