Solution
-
Call rand6() twice and generate two numbers , let us call them \(num_1\) and \(num_2\)
\(num_1 = rand6()\)
\(num_2 = rand6()\)
- Generate the output number based on the below formula
\begin{align}
output = \left\{
\begin{array}{cl}
num_1 + 6 & (if\quad num_2\quad is \quad even) \\
num_1 & otherwise.
\end{array}
\right.
\end{align}
Explanation of the above formula
If above formula is correct then probability of getting any number between 1-12 should be uniform i;e, p = 1/12.
Let's Estimate Probabilities
-
Case 1 : If num_2 is odd, then num_1 can be anything \([1-6] \). In this case output number will be \([1,6]\) with
\begin{align}
P(num_1 = \quad [1-6]) * P(num_2 \quad is \quad odd) = \frac{1}{6} * \frac{3}{6} = \frac{1}{12}
\end{align}
-
Case 2 : if num_2 is even, then num_1 can be anything \([1,6]\). In this case output number will be \([7,12]\) with
\begin{align}
P(num_1 = \quad [1-6]) * P(num_2 \quad is \quad even) = \frac{1}{6} * \frac{3}{6} = \frac{1}{12}
\end{align}
import numpy as np
# Function that returns random numbers from 1 to 6 with equal probability
def rand6():
return np.random.randint(1, 6)
# The function uses rand6() to return random numbers from 1 to 12 with equal probability
def rand12():
num = None
# Write your code
num = rand6() + (rand6()%2 ) *6
return num