Member-only story
How to Simulate a Coin Toss in R
The power of a simple function
I wonder if one day in our lifetimes, someone will ask us what a “quarter” is. It’s not totally out of the question.
Right now, the concept of loose change is still widely known, so let’s use the coin toss example to illustrate how you can calculate the probability of a certain number of heads (and tails) easily and efficiently.
Important Note
If you’re new to coding, I want to pause and underscore this:
Programming can be daunting. If you feel anxious about typing code or your imposter syndrome kicks in full-blast, take a deep breath, and remember that if you can learn how to speak a language (which, if you’re reading this- you did), you can learn how to write code. In the latter case, you’re just learning how to communicate with a computer.
For this example, the language we’re using to do that is R.
The Task: Simulate Coin Flips
We want R to get the computer to simulate a certain number of coin tosses. I’ll be using RStudio Cloud for my examples.
The R function we use in order to simulate a coin toss is