python翻转_Python程序设计偏向硬币翻转功能

python翻转

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.

在这里,我们将使用python构建biasedcoin()函数 。 该程序非常简单,只是一个入门程序,类似于用于定义偏置硬币翻转的功能coin() 。 该函数将使用命名为random的内置库。 这个随机的python库可帮助我们在范围内选择变量的随机值,或从给定的集合中获取一些随机值。

    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.

调用random.choice()函数时,集合中的每个成员都有相等的概率被解雇,并且如果一个成员在集合中多次出现,其概率也会增加。

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


翻译自: https://www.includehelp.com/python/program-to-design-a-biased-coin-flip-function.aspx

python翻转

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值