mean(replicate(10000, {dieroll <- sample(1:6, 2, replace = TRUE); dieroll[1] == 2*dieroll[2] | 2*dieroll[1] == dieroll[2]}))
## [1] 0.1668
As shown above, 10000 replications of 2 rolls are made and the probability of the chances one if twice the size of the other is found.
mean(replicate(10000, {dieroll <- sample(1:6, 2, replace = TRUE); abs(dieroll[1] - dieroll[2]) == 0}))
## [1] 0.1656
Again, 10000 replications of 2 rolls of a dice are made and this time the two values are subtracted from each other and then the absolute value is taken (so the sum is the same regardless of which order they were subtracted in) and the probability is found if the result is zero
mean(replicate(10000, {dieroll <- sample(1:6, 2, replace = TRUE); abs(dieroll[1] - dieroll[2]) == 4}))
## [1] 0.1121
The code in this part is the same as above except the probability is found when the result is 4 instead of zero. Again the absolute values are taken so that the order the numbers are subtracted from each other in doesn’t matter.
mean(replicate(10000, {sum(sample(0:1, 7, replace = TRUE)) == 3}))
## [1] 0.2701
10000 flips of 7 coins are simulated with heads given a value of 1 and tails a value of zero. When the sum of all 7 are added together the value is equal to the number of heads, so 3 heads results in a sum of 3.
mean(replicate(10000, {dieroll <- sample(1:6, 2, replace = TRUE); sum(dieroll) == 10}))
## [1] 0.079
Above the probability is found in the same fashion as the previous problems.
mean(replicate(10000, {dieroll <- sample(1:6, 5, replace = TRUE); (sum(dieroll)>15)&(sum(dieroll)<20)}))
## [1] 0.3922
Again, the probability is found in the same fashion, this time for values between 15 and 20
mean(replicate(10000, {dieroll <- sum(sample(1:6, 10, replace = TRUE)) == 20}))
## [1] 8e-04
Since the number of tosses of the die in the problem is not specified a value of 10 was chosen and the probability of the sum of these 10 tosses equalling 20 was found.
If 100 balls are randomly placed into 20 urns, estimate the probability that at least one of the urns is empty. The sample function allows for a range of values to be set for x and a number of samples with the size, but there isn’t a way to make sure that the total number of balls divided into the 20 urns doesn’t exceed 100 with only the replication, mean and sample functions, so I could not do this problem
Suppose a die is tossed three times. Let AA be the event “The first toss is a 5”. Let BB be the event “The first toss is the largest of the three”. Determine, via simulation or otherwise, whether AA and BB are independent.
mean(replicate(10000, {dieroll <- sample(1:6, 3, replace = TRUE); (dieroll[1]=5) & (dieroll[1]>dieroll[2]) & (dieroll[1]>dieroll[3])}))
## [1] 0.4414
AA and BB are independent.