Recca Chao 的 gitHub page

推廣網站開發,包含 Laravel 和 Kotlin 後端撰寫、自動化測試、讀書心得等。Taiwan Kotlin User Group 管理員。

View on GitHub

Problem 66

Description

This problem was asked by Square.

Assume you have access to a function toss_biased() which returns 0 or 1 with a probability that’s not 50-50 (but also not 0-100 or 100-0). You do not know the bias of the coin.

Write a function to simulate an unbiased coin toss.

Solutions

這題目的重點,是怎麽用 toss_biased() 組合出機率相同的兩個事件。

假設 toss_biased() 出現 0 的機率是 a,出現 1 的機率是 1-a,那麼我們連續呼叫兩次的話,機率如下:

a. 連續兩次出現 0 的機率是 a^2 b. 第一次出現 0,第二次出現 1 的機率是 a(1-a) c. 第一次出現 1,第二次出現 0 的機率是 a(1-a) d. 連續兩次出現 1 的機率是 (1-a)^2

我們只要把 b. 視為「拋出 0」,c. 視為拋出 1,其他情境重跑即可。

參考解法