Home » Python » Python programs

# Python program to design a biased coin flip function

**An example of random.choice() in Python**: Here, we are going to learn **how to design a function that can be used as biased coin flip** and the function will return a random value of biased coin flip?

Submitted by Anuj Singh, on August 01, 2019

Here, we are going to build a **biasedcoin() function using python**. The program is so simple as an introductory program and similar to the function **coin()** for defining a biased coin flip. The function is going to use an inbuilt library naming **random**. This random python library helps us to choose a random value of the variable within the range or take some random value from a given set.

random.choice(['H','T','H'])

The above function will choose a random value with a probability of:

COIN FLIP = PROBABILITY OF OCCURRENCE - HEAD = 0.67 - TAIL = 0.34

Each member of the set have equal probability to get fired when **random.choice() function** is called and if a member is present multiple times in the set, its probability increases as well.

**Here is the code**

import random # function to return the randon value # on biased biased coin FLIP def biasedcoin(): return random.choice(['H','T','H']) # main code i.e. function calling print('COIN FLIP : ', biasedcoin()) print('COIN FLIP : ', biasedcoin()) print('COIN FLIP : ', biasedcoin()) print('COIN FLIP : ', biasedcoin()) print('COIN FLIP : ', biasedcoin())

**Output**

COIN FLIP : H COIN FLIP : T COIN FLIP : H COIN FLIP : H COIN FLIP : H

TOP Interview Coding Problems/Challenges

- Run-length encoding (find/print frequency of letters in a string)
- Sort an array of 0's, 1's and 2's in linear time complexity
- Checking Anagrams (check whether two string is anagrams or not)
- Relative sorting algorithm
- Finding subarray with given sum
- Find the level in a binary tree with given sum K
- Check whether a Binary Tree is BST (Binary Search Tree) or not
- 1[0]1 Pattern Count
- Capitalize first and last letter of each word in a line
- Print vertical sum of a binary tree
- Print Boundary Sum of a Binary Tree
- Reverse a single linked list
- Greedy Strategy to solve major algorithm problems
- Job sequencing problem
- Root to leaf Path Sum
- Exit Point in a Matrix
- Find length of loop in a linked list
- Toppers of Class
- Print All Nodes that don't have Sibling
- Transform to Sum Tree
- Shortest Source to Destination Path

Comments and Discussions

**Ad:**
Are you a blogger? Join our Blogging forum.