模擬 A 與 B 同時發生的機率
你也可以用隨機模擬來估計兩個事件同時發生的機率。
本練習屬於課程
R 的機率基礎
練習說明
- 隨機模擬 100,000 次硬幣 A 的擲出結果,每次有 40% 的機率為正面。將結果存成變數
A。 - 隨機模擬 100,000 次硬幣 B 的擲出結果,每次有 20% 的機率為正面。將結果存成變數
B。 - 使用「且」運算子(
&)結合變數A與B,以估計 A 與 B 同時為正面的機率。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Simulate 100,000 flips of a coin with a 40% chance of heads
A <-
# Simulate 100,000 flips of a coin with a 20% chance of heads
B <-
# Estimate the probability both A and B are heads