集智社区
资源加载中,请稍后...
集智社区

集智社区

Sic Parvis Magna

求助(已解决)

白学家的迷茫与困惑-。-《python中random函数》

发表至编程讨论

本人在学习python的过程中遇到了一个非常奇怪的问题,random()函数不太理解用法。

本着自食其力的思想,我查阅了相关文档,得到如图答案

查到了文档,又了解到了新的知识,这本来是两件事很开心的事情 可是,为什么…… 会这个样子呢?

另外,会想要学习random的用法是因为在学习蒙特卡罗模拟比赛胜率的时候遇到了理解不能的问题。




#  Racquetball best of n games

from random import random

def main():
    printIntro()
    probA, probB, n, matches = getInputs()
    winsA, winsB = simMatches(matches, n, probA, probB)
    printSummary(winsA, winsB)

def printIntro():
    # Prints an introduction to the program
    print("This program simulates racquetball matches between two")
    print('players called "A" and "B".  The abilities of each player is')
    print("indicated by a probability (a number between 0 and 1) that")
    print("the player wins the point when serving. Player A always")
    print("serves first in the first game of a match, and the first")
    print("service alternates in subsequent games. \n")

def getInputs():
    # RETURNS probA, probB, number of games to simulate
    a = float(input("What is the prob. player A wins a serve? "))
    b = float(input("What is the prob. player B wins a serve? "))
    n = int(input("How many games are in a match? "))
    m = int(input("How many matches should be simulated? "))
    return a, b, n, m

def simMatches(howMany, n, probA, probB):
    # Simulates n games of racquetball between players A and B
    # RETURNS number of wins for A, number of wins for B
    winsA = winsB = 0
    for i in range(howMany):
        gamesA, gamesB = simOneMatch(n, probA, probB)
        if gamesA > gamesB:
            winsA = winsA + 1
        else:
            winsB = winsB + 1
    return winsA, winsB

def simOneMatch(n, probA, probB):
    gamesA = gamesB = 0
    needed = n/2 + 1
    while gamesA < needed and gamesB < needed:
        # Alternate ser

我并不能理解输入的a,b两名球员胜率到底是如何影响到随机函数的概率了。。。。简单的说,懵逼了。。。 所以,在有人出来打死我这个白学家之前,能不能告诉我,a,b两个赋值的运动员胜率如何参与了蒙特卡罗模拟哇?

回复
0
#1
simbol5月前

上面的报错问题因该是因为random.randrange()中参数必须是整数;Raises ValueError if stop <= start and number is non- integral.。可以查看randrange() in Pythona,b两个赋值的运动员胜率如何参与了蒙特卡罗模拟哇?这个问题就

回复
0
#2
蛆阁下5月前
@simbol

上面的报错问题因该是因为random.randrange()中参数必须是整数;Raises ValueError if stop <= start and number is non- integral.。可以查看randrange() in Pythona,b两个赋值的运动员胜率如何参与了蒙特卡罗模拟哇?这个问题就

非常感谢!昨天其实已经意识到这个问题了,只不过太晚了就忘记登陆论坛更新状态了。 另外谢谢推荐这个网址,我会好好利用的,再次感谢~!

回复
0
#3
@simbol

上面的报错问题因该是因为random.randrange()中参数必须是整数;Raises ValueError if stop <= start and number is non- integral.。可以查看randrange() in Pythona,b两个赋值的运动员胜率如何参与了蒙特卡罗模拟哇?这个问题就

啊久违的互助党,甚至想送礼品卡

回复
0
#4
simbol4月前

来啊,用礼品卡砸我

回复
0
#5
@simbol

来啊,用礼品卡砸我

TBN39EXSZNK9

回复
0
#6
simbol4月前

thankyou

最新回复:收起讨论