最大化产品
Maximize the Product
第 1 小问
题目详情
假设你掷出两个公平的 6 面骰子。两个骰子的乘积的期望值是多少?
Suppose you roll two fair 6-sided dice. What is the expected value of the product of the two dice?
解析
由于两个卷是独立的,因此产品的期望值是期望值的乘积:
Original Explanation
Since the two rolls are independent, the expected value of the product is the product of the expected values:
第 2 小问
题目详情
假设你有两个公平的 6 面骰子,并且你正在尝试通过可选的重掷来最大化产品。最佳游戏的预期值是多少?
Suppose you have the two fair 6-sided dice and you are trying to maximize the product with an optional re-roll. What is the expected value with optimal play?
解析
假设 是具有较高值的骰子。在最佳玩法下,我们永远不会重新掷两个骰子中的最大值。为了找到最大值的预期值,让我们计算一个值可能成为最大值的方式的数量。对于 ,你必须掷出两个 。对于 2 作为你的最大值,你只能滚动 和 ,这为你提供了 方法。 (这包括 情况,因此减去以避免重复计数)继续此过程以获得: 然后我们可以使用类似的计算来获得 的期望值,即最小值。然而,如果 低于重新掷骰的预期值,我们会用 替换该值。因此, 因为这些骰子彼此独立,所以我们可以将期望相乘,得到:
import random
from typing import Callable
roll_dice: Callable[[None], int] = lambda: random.randrange(1, 7)
products = []
for i in range(1000000):
d1 = roll_dice()
d2 = roll_dice()
product = d1 * d2
if min(d1, d2) < 3.5:
reroll = roll_dice()
product = max(d1, d2) * reroll
products.append(product)
#expecting something close to 16.83
print(sum(products) / len(products))Original Explanation
Suppose is the die with the higher value. With optimal play, we would never re-roll the maximum of the two dice. To find the expected value of the maximum, let's count the number of ways a value could be a maximum. For , you must roll two . For 2 to be your maximum, you can only roll 's and which gives you ways. (This includes case so subtract to not double count) Continue this process to get:
We can then use a similar computation to get the expected value of , the minimum. However, in the case the is below the expected value of a re-roll, we replace that value with the . Thus,
Because these die rolls were indendent of each other, we can multiply the expectations resulting in:
import random
from typing import Callable
roll_dice: Callable[[None], int] = lambda: random.randrange(1, 7)
products = []
for i in range(1000000):
d1 = roll_dice()
d2 = roll_dice()
product = d1 * d2
if min(d1, d2) < 3.5:
reroll = roll_dice()
product = max(d1, d2) * reroll
products.append(product)
#expecting something close to 16.83
print(sum(products) / len(products))