Dice

Contents

Dice#

P(4 dices sum to even)?#

0.5

How many times roll a dice until a 4 appears?#

Mathematically this is

\[ \begin{align}\begin{aligned}\begin{split} 1 \times 1/6 + 2 \times 5/6 \times 1/6 + 3 \times (5/6)^2 \times 1/6 + ... \\\end{split}\\\begin{split}= \frac{1}{6} \sum_{n=1}^{\infty} n (\frac{5}{6})^{n-1} \\\end{split}\\= \frac{1}{6} \times 36 = 6 \end{aligned}\end{align} \]
import random

counts = []
N = 100000
for _ in range(N):
    count = 0
    while random.choice(list(range(1,7))) != 4:
        count += 1
    counts.append(count+1) # plus final throw to get 4

sum(counts)/N
6.01285

Price a 3 call for the outcome of a 12 sided die#

1/12 * (1 + 2 + 3 + .. + 9) = 45/12 = 3.75

Price a 7 straddle for the outcome of the sum of two 6 sided dice#

p(sum = 6 or 8) = 5/36
p(sum = 5 or 9) = 4/36 ..

ev = 2 * ( 5/36 * 1 + 4/36 * 2 + 3/36 * 3 + 2/36 * 4 + 1/36 * 5) = 70/36 = 1.94

Expectation of how many times needed to get all sides of a dice at least once#

this is 1+6/5+6/4+6/3+6/2+6=14.7

import numpy as np
count_list = []
N = 10000
for _ in range(N):
    rolled = set()
    count=0
    while len(rolled) < 6:
        rolled.add(np.random.randint(1, 7))
        count += 1

    count_list.append(count)

print(sum(count_list)/N)
14.763

Start at the origin of the number line and repeatedly roll a die, roll = X. If the roll is 1, 2, or 3, you move X steps to the right. If the roll is 4, 5, or 6, you move (X − 3) steps to the left. On average, how many rolls will it take before you are at least 10 steps away from the origin?#

24.1

import random

def trial():
    pos=0
    t=0
    while abs(pos) < 10:
        x = random.randint(1,6)
        if x<=3:
            step = x
        else:
            step = -(x-3)
        pos += step
        t+=1
    return t

N=200_000
sum(trial() for _ in range(N))/N
24.035075

Two dice are rolled. What is the probability the difference of the two numbers shown is greater than 2?#

There are 36 equally likely outcomes when rolling two fair dice.

We count the outcomes where the absolute difference (|a - b| > 2):

  • Difference = 3:
    (1,4), (2,5), (3,6), (4,1), (5,2), (6,3) → 6 outcomes

  • Difference = 4:
    (1,5), (2,6), (5,1), (6,2) → 4 outcomes

  • Difference = 5:
    (1,6), (6,1) → 2 outcomes

Total favorable outcomes = 6 + 4 + 2 = 12

Probability = 12/36 = 0.33

You roll 30 dice. What is the probability that at least 5 of these are 6?#

Let \(X\) be the number of sixes. Then:

\[ X \sim \text{Binomial}(30, \tfrac{1}{6}). \]

We want:

\[ P(X \ge 5) = \sum_{k=5}^{30} \binom{30}{k} \left(\frac{1}{6}\right)^k \left(\frac{5}{6}\right)^{30-k}. \]

Evaluating the binomial tail gives:

\[ P(X \ge 5) \approx 0.576. \]

100 dice, remove all odd numbers, ev of sum of dice left?#

EV(even dice) = 4
EV(num of dice) = 50

50 * 4 = 200

You roll three fair six-sided dice, EV of a payoff of \(2^{\,a + b + c}\)?#

Because the dice are independent:

\[ E\left[2^{a+b+c}\right] = E\left[2^a \cdot 2^b \cdot 2^c\right] = E[2^a] \, E[2^b] \, E[2^c] = (E[2^X])^3, \]

where (X) is the result of a single die roll.

Compute the expectation for one die:

\[ E[2^X] = \frac{1}{6} (2^1 + 2^2 + 2^3 + 2^4 + 2^5 + 2^6) = 126/6 = 21 \]

so ans

\[ 21^3 = 9261. \]

6 dice, prob of getting at least 2 pairs#

0.6

p(roll 6 dice, get at least 3 of same number)#

0.3673

from collections import Counter

def simulate_dice_prob(num_trials=1_000_000):
    success_count = 0
    
    for _ in range(num_trials):
        rolls = [random.randint(1, 6) for _ in range(6)]
        counts = Counter(rolls)
        if any(count >= 3 for count in counts.values()):
            success_count += 1
            
    return success_count / num_trials

estimated_prob = simulate_dice_prob()
theoretical_prob = 119 / 324

print(f"Monte Carlo Estimate (1M trials): {estimated_prob:.5f}")
Monte Carlo Estimate (1M trials): 0.36756

start with 30, roll 10 dice, if even then add else subtract, final ev?#

ev per round is 0.5 * 4 + 0.5 * -3 = 0.5

30 + 10 * 0.5 = 35

Starting with 30, after 10 throws:

  • Probability of Bankruptcy: 0.23%

  • Expected Final Wealth: 35.02

start with 10, 10 dice throws, if even then added, odd then subtracted, p (bankrupt at some point)?#

Starting with 10, after 10 throws:

  • Probability of Bankruptcy: 23.40%

  • Expected Final Wealth: 14.43

EV of each move is 0.5, Var of each move is approx 15, so 10 steps amount to about N(5, 150), where std is about 12. Then P( N(5, 150) <= -10) is about 1.25 std from mean, which is about 0.1. this is the probability of going <0 after 10 steps, Reflection Principle states that the probability of ever crossing a barrier is exactly twice the probability of ending below it, so we should estimate about 0.2

You keep rolling a fair six-sided die and you keep track of 6 different sums:#

the sum of all the 1s, the sum of all the 2s, the sum of all the 3s, the sum of all the 4s, the sum of all the 5s, and the sum of all the 6s. The game ends once any one of these sums exceeds 100. What is the expected value of the number of even numbers that have been rolled?

  • EV(num of even rolled) = 47.8

  • EV(total sum) = 334

— Simulation Results — Trials run: 100000 Average (Expected) number of even numbers rolled: 47.8524 Average (Expected) total sum when game ends: 334.8959 Average (Expected) even sum when game ends: 191.3958 Average (Expected) odd sum when game ends: 143.5001

We would exceed 100 when we have 16 6s rolled. By symmetry about 16 2s and 4s are rolled, so this is 48 even numbers rolled.

roll a dice, keep the sum util hitting a number exceeding 100, prob of last row was 2?#

0.1

Think of a long road with some parts make of segments of size 1, some of size 2… some of size 6 randomly. The probability of the 100th meter falling on a size-2 segment is 2/(1+2+…+6) = 2/21

def trial():
    s = 0
    while True:
        r = random.randint(1, 6)
        s += r
        if s > 100:
            return r

N = 1_000_00
count = sum(1 for _ in range(N) if trial() == 2)
count / N
0.09601

You roll a die continuously until hit a number greater than 4. At that point, the game ends and you are paid the sum of all rolls. What is the expected value of the game?#

expected number of throws = 3

10.5

You roll a die continuously until you roll the same number twice in a row. At that point, the game ends and you are paid the sum of all rolls. What is the expected value of the game?#

expected number of throws = 7:

E = 1 + 1 + 5/6(E - 1)

then ev = 7 * 3.5 = 24.5

You roll a die continuously until you roll the same number twice in a row. At that point, the game ends and you are paid the product of all rolls. What is the expected value of the game?#

choices: 200, 2000, 20000

EV is infinite, does not converge

def trial(max_rolls=10_000):
    rolls = []
    prev = None
    for _ in range(max_rolls):
        r = random.randint(1, 6)
        rolls.append(r)
        if r == prev:
            prod = 1
            for x in rolls:
                prod *= x
            return prod
        prev = r
    return None  # did not terminate (rare)

N = 200_000
vals = []
for _ in range(N):
    v = trial()
    if v is not None:
        vals.append(v)

# basic stats
mean = sum(vals) / len(vals)
median = sorted(vals)[len(vals)//2]

mean, median
(1.3600441642982847e+25, 360)

roll 100 dice, repeatedly remove sets of 6 distinct dice until no longer possible. ev of sum of what is left#

intuition: around 11 sets of 1-6, after removal, 34 dice left, sum of 34*3.5 = 119

ans = 104.8

3 dice are rolled, probbaility some number of thsese dice (one dice, two or three) can be summed to 5?#

0.67

def trial():
    dice = [random.randint(1, 6) for _ in range(3)]
    for mask in range(1, 1 << 3):
        if sum(dice[i] for i in range(3) if mask & (1 << i)) == 5:
            return True
    return False

N = 1_000_000
count = sum(trial() for _ in range(N))
count / N
0.671436

a particle is on vertex of a decagon (10 vertices). Each second, moves clockwise if dice is 1 or 2, anticlockwise if 3 or 4, stay if 5 or 6. Expected time to reach opposite vertex (vertex furthest from initial)?#

intuition:
Let starting vertice be 0, opposite vertice be 5, all vertices be 0-9

clearly \(E_0 = 1 + 1/3 E_1 + 1/3 E_9 + 1/3 E_0 \), note \(E_9 = E_1\) by symmetry,
so \(2/3 E_0 = 1 + 2/3 E_1 \), \( E_0 = 3/2 + E_1\)

similarly \(E_1 = 1 + 1/3 E_2 + 1/3 E_0 + 1/3 E_1 \),
plug in to have \(E_1 = 9/2 + E_2 \)
similarly \(E_1 = 15/2 + E_2\)

can conclude \(E_0 = 3/2 + 9/2 + 15/2 + 21/2 + 27/2 + E_5 = 75/2 = 37.5\)

import numpy as np

def decagon_simulate():
    pos = 0                 # start vertex
    target = 5              # opposite vertex on a decagon
    t = 0
    while pos != target:
        roll = random.randint(1, 6)
        if roll in (1, 2):          # clockwise
            pos = (pos + 1) % 10
        elif roll in (3, 4):        # anticlockwise
            pos = (pos - 1) % 10
        # else: stay
        t += 1
    return t

times = [decagon_simulate() for _ in range(10000)]
np.mean(times)
37.6652