Riddler Express
Itβs time for a random number duel! You and I will both use random number generators, which should give you random real numbers between and . Whoeverβs number is greater wins the duel!
Thereβs just one problem. Iβve hacked your random number generator. Instead of giving you a random number between and , it gives you a random number between and .
What are your chances of winning the duel?
Solution
The random generator that I have generates numbers according to where and the random generator that you have (in the general case) generates numbers according to . We need the probability, . This is given by the area of the trapezium divided by the area of the rectangle . We have , , and in the figure below
In our particular case and , therefore the probability of me winning is .
Computational validation
The probability of me winning the duel as per the simulation below is which validates the result we got earlier.
from numpy.random import uniform
def prob_win(p1l, p1h, p2l, p2h, runs = 1000000):
total_wins = 0
for _ in range(runs):
x, y = uniform(p1l, p1h), uniform(p2l, p2h)
if x > y:
total_wins += 1
return total_wins/runs
print(prob_win(0.1,0.8,0,1))
Riddler Classic
I have in my possession 1 million fair coins. Before you ask, these are not legal tender. Among these, I want to find the βluckiestβ coin.
I first flip all 1 million coins simultaneously (Iβm great at multitasking like that), discarding any coins that come up tails. I flip all the coins that come up heads a second time, and I again discard any of these coins that come up tails. I repeat this process, over and over again. If at any point I am left with one coin, I declare that to be the βluckiestβ coin.
But getting to one coin is no sure thing. For example, I might find myself with two coins, flip both of them and have both come up tails. Then I would have zero coins, never having had exactly one coin.
What is the probability that I will β at some point β have exactly one βluckiestβ coin?
Solution
Let be the probabillity that we will have exactly one βluckiestβ coin starting with coins. We have the following recurrence relation:
because the probability of ending up with heads when you flip coins is and then it is equivalent to starting the game with coins. When , .
Computational Solution
From the code below, we get . Assuming convergence,
from functools import lru_cache
from math import comb
def prob_luckiest_coin(n):
@lru_cache()
def prob(n):
if n == 1:
return 1
else:
total_prob = 0
for i in range(1, n):
total_prob += prob(i)*comb(n,i)
return total_prob/(2**n-1)
return prob(n)
print(prob_luckiest_coin(100))