# Combination - Passwords & Students

> This document is written in *R*.
>
> ***GitHub***: https://github.com/czs108

## Question A

> If passwords can consist of **6** *letters*, find the probability that a randomly chosen password will *not* have any *repeated* letters.

\begin{equation}
P = \frac{26 \times 25 \times \cdots \times 21}{26^{6}}
\end{equation}


```R
norpt <- 0
count <- 100000
for (i in c(1:count)) {
    pwd <- sample(x=letters, size=6, replace=TRUE)
    if (length(unique(pwd)) == 6) {
        norpt <- norpt + 1
    }
}

norpt / count
```


0.53777


## Question B

> How many ways can you get a sample of **6** letters *without* *repeated* letters, if the order does *not* matter?

\begin{equation}
^{26}C_6 = \frac{26!}{6! \times 20!}
\end{equation}


```R
choose(n=26, k=6)
```


230230


## Question C

> Use the `sample` command to simulate tossing a coin **10000** times. You can use a `for` loop and record the result of each toss. Then you can the use `table` command to find how often you got *heads* or *tails*.


```R
res <- sample(x=c("Head", "Tail"), size=10000, replace=TRUE)

prop.table(table(res))
```


    res
      Head   Tail 
    0.5015 0.4985 


## Question D

> If a class contains **60** *females* and **40** *males* and you choose a random sample of **5** students from the class, what is the probability of getting **5** *females*?

\begin{equation}
P = \frac{^{60}C_5}{^{100}C_{5}}
\end{equation}


```R
choose(n=60, k=5) / choose(n=100, k=5)
```


0.0725420627482483


Check the *Hypergeometric Distribution*.


```R
dhyper(x=c(0:5), m=60, n=40, k=5)
```


<ol class=list-inline>
	<li>0.00873993458676817</li>
	<li>0.072832788223068</li>
	<li>0.232277540819514</li>
	<li>0.354528878092943</li>
	<li>0.259078795529458</li>
	<li>0.0725420627482483</li>
</ol>



## Question E

> Use the `sample` command to simulate the situation in *Question D*. Repeat the sample **10000** times. How often do you get **5** *females*?


```R
students <- c(rep("Male", 40), rep("Female", 60))
noman <- 0
count <- 10000
for (i in c(1:count)) {
    group <- sample(x=students, size=5)
    if (group[1] == "Female" && length(unique(group)) == 1) {
        noman <- noman + 1
    }
}

noman / count
```


0.0771


351dc578b236a2d8657c6e3e7373b9c704ee3c85
ipynb
Jupyter Notebook
exercises/Combination - Passwords & Students.ipynb
czs108/Probability-Theory-Exercises
60c6546db1e7f075b311d1e59b0afc3a13d93229
MIT
exercises/Combination - Passwords & Students.ipynb
czs108/Probability-Theory-Exercises
60c6546db1e7f075b311d1e59b0afc3a13d93229
MIT
exercises/Combination - Passwords & Students.ipynb
czs108/Probability-Theory-Exercises
60c6546db1e7f075b311d1e59b0afc3a13d93229
MIT
2022-03-21T05:04:07.000Z
2022-03-21T05:04:07.000Z
Qwen/Qwen-72B
1. YES
2. YES