Basic algorithms 101 : Coin toss (unbiased and biased)

Welcome to the first post of the new series “simple algorithms”. In this series we will be dealing with simple to understand programming algorithms. If you are a beginner at programming or aspiring to be a programmer, this series is meant for you.  For convenience we will be taking python as an example for the code.
In this first post, will be dealing with a very simple algorithm which is “The Coin Toss algorithm”. In case you don’t know a coin has two sides, a head and a tail. When you flip it, the outcome is either a head or a tail.  As you can see, there are only two cases. So that makes it very easy.  Some people might want to know the algorithm for a biased coin. So we will be looking at that too. The biased one is slightly trickier than the unbiased one. First, let’s look at the unbiased and then get to the biased coin.  So without any further ado, let’s just get right into it.


Unbiased coin algorithm:
  1. Start
  2. Create an array ”coin” containing two elements: [“head”,”tail”]
  3. Generate a random ‘i’ from 0 to 1 (both included).
  4. Now use this number as index to access a random element from “coin”
  5. Output the ith element as the outcome of the coin ( coin[i])
  6. Stop 

This basically selects head or tail randomly. That is the whole algorithm. The probability of getting either a head or a tail is both 50%. Just like a real coin. So this was the easy one.






Biased coin algorithm:
  1. Start
  2. Get the percentage of occurrence ‘p’ of the head.
  3. Generate a random number ‘i’ from 1 to 10 (both included).
  4.  If ‘i’ is less than or equal to p/10, then output head as the outcome.
  5. Else, output tail as the outcome.
  6. Stop
Here, we require the percentage of occurrence. It need not just be the percentage of occurrence of head. It could be tail too. This algorithm has the same number of steps as the previous one. But it’s a bit more complex as I mentioned. Here we are generating a random number from 1 to 10 which will help us get the required probability.  Try and understand how this algorithm works. It works properly. There are many other ways to do this. But this is this is the best method I could think of. If you have a better one please share it in the comments below.

Code in python:

def unbiasedCoin():
             import random
coin = [‘head’,’tail’]
i = random.randrange(0,2)
return coin[i]
def biasedCoin():
                import random
                p = 70
                i = random.randrange(1,11)
                if i <= (p/10) :
                                return ‘Head’
                return ‘Tail’
               
So this is it for the post. I hope you could understand the logic for both the cases. If you have any queries, please do not hesitate to ask in the comments below or through the contact page. Hope this helped. I will be coming up with a lot more algorithms in the coming days. If you have any suggestions, please comment below or use ‘contact us’. I will try to solve it myself or get help from people who can. The next post in this series will be on ‘Dice roll’. Don’t forget to subscribe.

Next in the series basic algorithms 101 is : Checking validity of the given email address

Thank you