Q:

Python program to design a biased dice throw function

belongs to collection: Python basic programs

0

Here, we are going to build a biaseddice() function using python. The program is so simple as an introductory program and similar to the function dice() for defining a function. 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([1,2,3,4,4,4,5,6,6,6])
    

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

        DICE FACE = PROBABILITY OF OCCURRENCE
        -    1 = 0.1
        -    2 = 0.1
        -    3 = 0.1
        -    4 = 0.3
        -    5 = 0.1
        -    6 = 0.3

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Here is the code

import random

# function to return the randon value 
# on biased dice roll
def biaseddice():
    return random.choice([1,2,3,4,4,4,5,6,6,6])

# main code i.e. calling the function
print('DICE THREW : ', biaseddice())
print('DICE THREW : ', biaseddice())
print('DICE THREW : ', biaseddice())
print('DICE THREW : ', biaseddice())
print('DICE THREW : ', biaseddice())
print('DICE THREW : ', biaseddice())
print('DICE THREW : ', biaseddice())
print('DICE THREW : ', biaseddice())
print('DICE THREW : ', biaseddice())
print('DICE THREW : ', biaseddice())

Output

DICE THREW :  6
DICE THREW :  4
DICE THREW :  5
DICE THREW :  6
DICE THREW :  2
DICE THREW :  2
DICE THREW :  6
DICE THREW :  4
DICE THREW :  4
DICE THREW :  3

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Python basic programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Python program to design a biased coin flip functi... >>
<< Python program to design a dice throw function...