This post explains the algorithm for programming a coin toss. It includes explanation of both biased and unbiased coins.
Read this post on teckguide.blogspot.com