Python program to design a biased coin flip function

An example of random.choice() in Python: Here, we will figure out how to structure a capacity that can be utilized as a one-sided coin flip and the capacity will restore an irregular estimation of a one-sided coin flip?

Here, we are going to manufacture a biasedcoin() work utilizing python. The program is so basic as an initial program and like the capacity coin() for characterizing a one-sided coin flip. The capacity is going to utilize an inbuilt library naming irregular. This arbitrary python library causes us to pick an irregular estimation of the variable inside the range or take some arbitrary incentive from a given set.

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

The above capacity will pick an arbitrary incentive with a likelihood of:

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

Every individual from the set have equivalent likelihood to get terminated when random.choice() work is called and if a part is available on various occasions in the set, its likelihood increments also.

Here’s 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

Leave a Comment

error: Alert: Content is protected!!