I have this code right here that simulates a coin flip and finds out how many times one can flip head before a tail comes up. I don’t full understand this code and have a few questions.
-
I know that rand picks either 0 or 1 but how does it get added with the ++ since adding 0 would result in the same number?
-
It says if($flip) echo H or T respectively. How does that code exactly work? How does the computer know 0 is head and 1 is tail?
$flipCount = 0;
do {
$flip = rand(0,1);
$flipCount ++;
if ($flip){
echo “H”;
}
else {
echo “T”;
}
} while ($flip);
$verb = “were”;
$last = “flips”;
if ($flipCount == 1) {
$verb = “was”;
$last = “flip”;
}
echo “There {$verb} {$flipCount} {$last}!
”;
Thanks a lot for your help!