繁体   English   中英

如何计算两个不同能力的玩家在 PARS 壁球比赛中获胜的概率? (Python)

[英]How can I calculate the probability that two players different abilities will win a PARS squash game against each other? (Python)

我创建了一个 function winProbability(ra, rb, n) 并且我想模拟 n 场比赛以估计具有能力 ra 的玩家将赢得与能力 rb 的玩家的比赛的概率

我将展示到目前为止我已经完成的代码。 如果这似乎是一个简单的问题,那是因为我是编码新手。

import random #import random 允许使用随机生成的数字

def game(ra, rb): #this function 游戏设置了游戏运行的方式 p_a_point = ra/(ra+rb) #这行代码决定了#player a 赢得任何给定点的概率

a_points = 0 #the amount of points player a has is defaulted to 0
b_points = 0 #the amount of points player b has is defaulted to 0

score_to_win = 11 #the winning score is defaulted to 11

while (a_points < score_to_win and b_points < score_to_win) or  abs (a_points - b_points) < 2: #while player a's points and player b's points are less than the winning score:
    p_b_point = random.random()#the probability b wins a point is set the a random value between 0 and 1
    if p_b_point < p_a_point: #is the probability b wins a point is less than the probability a wins a point:
        a_points = a_points + 1 #a wins 1 point
    else: #if player a doesn't win a point:
        b_points = b_points + 1 #b wins a point
return(a_points, b_points)#both players points are returned

print(game(70,30))#the function 以两个 integer 值作为参数调用

def winProbability(ra, rb, n):

老实说,从这里我不确定如何 go 关于这个。 我正在考虑做一个for循环,例如:for n in game (ra, rb):

但我不确定是否可以在此循环调用中使用以前的 function。 我也对如何计算代码中的概率感到困惑

总体目标是调用 function 有两个概率,例如 70 和 30,并给出玩家 ra 获胜的概率的十进制答案。

对于之前的评论者,我为之前的含糊道歉。 我以前从来没有在这里发过帖子。

看看这是否有帮助。

from random import randint, seed

seed()

rounds = input(" How many rounds will be played in the match? ")

print("\n Please enter skill levels as integers from 0 to 99.\n")
a = input(" What is the skill level of player 1? ")
b = input(" What is the skill level of player 2? ")


# Catch empty inputs
if not rounds: rounds = 10
if not a: a = 0
if not b: b = 0

# Python inputs are always strings. Convert them to integers.
rounds = int(rounds)
a = int(a)
b = int(b)

# If both skill levels are 0, set them to 1.
# (This will avoid a possible division by zero error.)
if a+b == 0: a = b = 1

# Catch and correct numbers that are too high.
if a > 99: a = 99
if b > 99: b = 99

# Convert integer skill levels to values between 0.0 and 0.99
a = a/100
b = b/100

print()
print(" Chance player 1 will win: "+str(int(100*a/(a+b)))+" percent.")
print(" Chance Player 2 will Win: "+str(int(100*b/(a+b)))+" percent.")
print()

for x in range(rounds):
    roll = randint(0,999)/1000
    print("roll =",roll, end =": ")
    if roll <= a/(a+b):  # <-- Compare roll to ratio of player skill levels.
        print("Round "+str(x+1)+" Winner: Player 1")
    else:
        print("Round "+str(x+1)+" Winner: Player 2")
print()

这是我的答案

import random

def winProbability(ra, rb, n):

    winCount = 0 #used to count how many times 'a' won
    probabilityRange = ra + rb

    for i in range(n):
        # pick a number between 0 and probabiilityRange
        number = random.randint(0, probabilityRange)

        # if number is < ra then 'a' won if number is > ra then 'b' won if number == ra then results in a draw
        if number < ra:
            winCount += 1
            print ('win')

        if number > ra:
            print('loss')

        if number == ra:
            print ('draw') # draw doesn't count as win

    return winCount*(100/n)

print (winProbability(10000,1,100000))

这将打印每场比赛的结果,并以百分位形式返回“a”获胜的可能性。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM