Empirical probability in R with x1+x2>2*x3

Viewed 76

I have a question - how to generate 100 times a vector of 3 random numbers (x1, x2, x3) from 1 to 10 so I can find the empirical probability of x1+x2>2*x3? I know how to generate 3 random numbers from 1 to 10 with sample(1:10, 3) but how to do this n times?

Thanks in advance

1 Answers

It's simple to repeat random draws with replicate (here showing 10 replications):

reps <- replicate(10, sample(1:10, 3))

     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,]    9    1    6    8    3    1   10    7    1     2
[2,]    8    9   10    2    4    7    5    3    5     5
[3,]    1    8    4    6    7    2    7    5    3     9

The empirical probability of your inequality can be calculated:

set.seed(1222)
reps <- replicate(100, sample(1:10, 3))
prob <- mean(reps[1, ] + reps[2, ] > reps[3, ] * 2)
[1] 0.39

Just for completeness, because there are only 1,000 possible combinations of x1, x2, and x3, we can generate all of them and calculate the exact probability:

all.probs <- expand.grid(1:10, 1:10, (1:10) * 2)
prob <- mean(all.probs[,1] + all.probs[,2] > all.probs[,3])
[1] 0.475
Related