How do you get exactly 50% odds?
Mathematically speaking, a test which is intended to split the interval [0,1)
(using [
as "inclusive" and )
as exclusive) in an exact 50-50 ratio would use a comparison like
if (Math.random() >= 0.5) ...
This is because this splits the initial interval [0,1)
into two equal intervals [0,0.5)
and [0.5,1)
.
By comparison, the test
if (Math.random() > 0.5) ...
splits the interval into [0,0.5]
and (0.5,1)
, which have the same length, but the first is boundary-inclusive while the second is not.
Whether the boundaries are included in the same way in both tests does not matter in the limit as the precision approaches infinite, but for all finite precision, it makes a minute but measurable difference.
Suppose the precision limit is 0.000001
(decimal), then the >=0.5
test has exactly [0,0.499999]
and [0.5,0.999999]
and it is plain to see that adding 0.5 to the first interval (or subtracting it from the second) makes the two intervals align perfectly. On the other hand, under this precision, the >0.5
test makes the intervals [0,0.5]
and [0.500001,0.999999]
which are clearly unequal in favor of the numbers <=0.5
. In fact, the ratio is then 500001:499999, which is obviously negligibly different from 50:50, but different all the same.