Simulating Coin Flips in R

Actually @FJCC's solution is not quite correct, since it operates on groups of four, not one long string. If there were an issue with the RNG that would affect patterns of more than four repetitions, this would not be recognized.

There is an important lesson in that: when doing these kinds of statistical simulations, you have to be careful to translate the question exactly, and not use code that you assume to be equivalent. OP has made an assumption about how str_count() works, that turned out to be wrong, and @FJCC made an assumption that four independently flipped elements would be equivalent to OP's scenario, but that's not exactly true (although their analysis of the reason why OP's result is wrong is spot on!). Of course the resulting code may turn out to be awkward - but otherwise, if we assume independence, we might just as well write table(sample(0:1, 1000000, replace=TRUE)) - and we all know how that will turn out.

:slightly_smiling_face: