Tossing Coins 🟡
Contents
Tossing Coins 🟡#
Write a function that works as a coin-flipper simulator. Then, write another function that calls the coin flipper function several times to determine the approximate probability of getting a certain result combination for
2
tosses (regardless of order).
Rules#
Coin-flipper simulator#
Coin type: 2 faces:
head
andtail
.Coin is fair: equal chance of flipping
head
ortail
.The function must return a
string
value corresponding to the tossed face (head
ortail
).You can use any
random function
from therandom
module.
Probability calculator#
Function that accepts
two
string
inputs corresponding to the desired tossed faces, where:The first input is necessary;
If no second input is given, it should should default to an empty string.
Function returns a
float
value between0
and1
.Avoid using any knowledge of combinatorics to solve this problem. Instead, take advantage of the fact that the machine can simulate millions of tosses (samples) in a very short time.
Note
Since this is based on random draws, the probability will be slightly different each time the code is run.
So, the greater the number of samples, the more consistent the results.
Example#
Head & Tail or Tail & Head#
If you call get_probability(head, tail)
or get_probability(tail, head)
, this
means you want to know the probability of drawing a head
and a tail
(regardless of order) when tossing two coins. The result should be 0.50
.
Head & Head#
If you call get_probability(head, head)
, this means you want to know the
probability of drawing two heads
when
tossing two coins. The result should be 0.25
.
At least one Head#
Finally, if you call get_probability(head)
, this means you want to know the
probability of drawing at least one head
when
tossing two coins. This includes head+tail
, tail+head
and head+head
, so
the result should be 0.75
.