-
Notifications
You must be signed in to change notification settings - Fork 5
/
HW_2_AH_EP.Rmd
120 lines (100 loc) · 3.69 KB
/
HW_2_AH_EP.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
---
title: "Homework_2_AH_EP"
author: "Andrea Hemmelmann - Eric Parra"
date: "18 de noviembre de 2018"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## Homework 2
Tsunami scientist Trevor rolls some dice and tells you that the added result is 17. How many dice did Trevor roll? Write some R code to find this out!
## Results
We know that the sum of the dice thrown is 17. Therefore, the minimum amount of dice needed to achieve that amount is 3 (6, 6, 5) and the maximum amount of dice is 17 (assuming that all the dice give as result 1).
Based on the above, we have performed the simulation of 100.000 rolls for each possible number of dice (In this exercise, we avoid using a loop to perform the simulations).
```{r }
n <- 100000
dice_3 <- replicate(n, (sum(sample(1:6, 3, replace = TRUE))))
dice_4 <- replicate(n, (sum(sample(1:6, 4, replace = TRUE))))
dice_5 <- replicate(n, (sum(sample(1:6, 5, replace = TRUE))))
dice_6 <- replicate(n, (sum(sample(1:6, 6, replace = TRUE))))
dice_7 <- replicate(n, (sum(sample(1:6, 7, replace = TRUE))))
dice_8 <- replicate(n, (sum(sample(1:6, 8, replace = TRUE))))
dice_9 <- replicate(n, (sum(sample(1:6, 9, replace = TRUE))))
dice_10 <- replicate(n, (sum(sample(1:6, 10, replace = TRUE))))
dice_11 <- replicate(n, (sum(sample(1:6, 11, replace = TRUE))))
dice_12 <- replicate(n, (sum(sample(1:6, 12, replace = TRUE))))
dice_13 <- replicate(n, (sum(sample(1:6, 13, replace = TRUE))))
dice_14 <- replicate(n, (sum(sample(1:6, 14, replace = TRUE))))
dice_15 <- replicate(n, (sum(sample(1:6, 15, replace = TRUE))))
dice_16 <- replicate(n, (sum(sample(1:6, 16, replace = TRUE))))
dice_17 <- replicate(n, (sum(sample(1:6, 17, replace = TRUE))))
```
Then, we counted the number of roll in which the total sum was 17.
```{r}
count_dice_3 <- length(which(dice_3 == 17))
count_dice_4 <- length(which(dice_4 == 17))
count_dice_5 <- length(which(dice_5 == 17))
count_dice_6 <- length(which(dice_6 == 17))
count_dice_7 <- length(which(dice_7 == 17))
count_dice_8 <- length(which(dice_8 == 17))
count_dice_9 <- length(which(dice_9 == 17))
count_dice_10 <- length(which(dice_10 == 17))
count_dice_11 <- length(which(dice_11 == 17))
count_dice_12 <- length(which(dice_12 == 17))
count_dice_13 <- length(which(dice_13 == 17))
count_dice_14 <- length(which(dice_14 == 17))
count_dice_15 <- length(which(dice_15 == 17))
count_dice_16 <- length(which(dice_16 == 17))
count_dice_17 <- length(which(dice_17 == 17))
count_dice_3
count_dice_4
count_dice_5
count_dice_6
count_dice_7
count_dice_8
count_dice_9
count_dice_10
count_dice_11
count_dice_12
count_dice_13
count_dice_14
count_dice_15
count_dice_16
count_dice_17
```
Finally, we obtained the probability of getting as a result 17 for each number of possible dice.
```{r}
p_dice_3 <- count_dice_3/n
p_dice_4 <- count_dice_4/n
p_dice_5 <- count_dice_5/n
p_dice_6 <- count_dice_6/n
p_dice_7 <- count_dice_7/n
p_dice_8 <- count_dice_8/n
p_dice_9 <- count_dice_9/n
p_dice_10 <- count_dice_10/n
p_dice_11 <- count_dice_11/n
p_dice_12 <- count_dice_12/n
p_dice_13 <- count_dice_13/n
p_dice_14 <- count_dice_14/n
p_dice_15 <- count_dice_15/n
p_dice_16 <- count_dice_16/n
p_dice_17 <- count_dice_17/n
p_dice_3
p_dice_4
p_dice_5
p_dice_6
p_dice_7
p_dice_8
p_dice_9
p_dice_10
p_dice_11
p_dice_12
p_dice_13
p_dice_14
p_dice_15
p_dice_16
p_dice_17
```
As you can see, the results show that the most probable amount of dice rolled to get a sum of 17 is 5 dice.
Finally, it draws our attention that from the simulation of the roll of 12 dice, the value 17 was not obtained, although we simulated 100.000 rolls.