python掷骰子
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.
在这里,我们将使用python构建biaseddice()函数 。 该程序非常简单,只是一个入门程序,类似于用于定义function的函数dice() 。 该函数将使用命名为random()的内置库。 这个随机的python库可帮助我们在范围内选择变量的随机值,或从给定的集合中获取一些随机值。
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
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.
调用random.choice()函数时,集合中的每个成员都有相等的概率被解雇,并且如果一个成员在集合中多次出现,其概率也会增加。
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
翻译自: https://www.includehelp.com/python/program-to-design-a-biased-dice-throw-function.aspx
python掷骰子