首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将变量中的偏差值合并到函数中?[python]

如何将变量中的偏差值合并到函数中?[python]
EN

Stack Overflow用户
提问于 2020-11-26 21:43:32
回答 1查看 77关注 0票数 0

我在模拟一个场景,一个自助洗衣店里有十双袜子,还有六只随机丢失的袜子。在此之后,可以至少剩下四对,最多七对。我编写了一个代码,它运行并模拟概率,但是当机会不相等时,它将所有的概率视为平等的机会。我的问题是,现在如何将偏见纳入这段代码:

代码语言:javascript
复制
import random #random number generator
# variables
number_of_pairs = 10 # how many pairs of socks there are originally
total_socks = number_of_pairs * 2 # how many total socks there are
socks_lost = 6 # how many socks were lost
number_of_simulations = 1000 # total number of trials being done
prob_of_7_pairs = (1/6) # probability that there are still seven pairs of socks left
prob_of_6_pairs = (2/9) # probability that there are still six pairs of socks left
prob_of_5_pairs = (5/18) # probability that there are still five pairs of socks left
prob_of_4_pairs = (1/3) # probability that there are still four pairs of socks left
four_pairs = 0 # how many times out of a thousand trials that there are still four pairs left
five_pairs = 0 # how many times out of a thousand trials that there are still five pairs left
six_pairs = 0 # how many times out of a thousand trials that there are still six pairs left
seven_pairs = 0 # how many times out of a thousand trials that there are still seven pairs left
# function
for i in range(0, number_of_simulations): # i is the trial runs, range is from 0 to how many trials there are
    possible_pairs = random.randint(4,7) # random number generator will randomly select between four and seven pairs of socks
    if possible_pairs == 4: 
        four_pairs += 1 # if there are only four possible pairs of socks in one of the trials, the program will add one to the total
    elif possible_pairs == 5:
        five_pairs += 1 # if there are only five possible pairs of socks in one of the trials, the program will add one to the total
    elif possible_pairs == 6:
        six_pairs += 1 # if there are only six possible pairs of socks in one of the trials, the program will add one to the total
    elif possible_pairs == 7:
        seven_pairs += 1 # if there are only seven possible pairs of socks in one of the trials, the program will add one to the total
# results
print('There were four possible pairs of socks', four_pairs, 'of times.') # prints out how many times 1 was rolled out of a thousand rolls
print('There were five possible pairs of socks', five_pairs, 'of times.') # prints out how many times 2 was rolled out of a thousand rolls
print('There were six possible pairs of socks', six_pairs, 'of times.') # prints out how many times 3 was rolled out of a thousand rolls
print('There were seven possible pairs of socks', seven_pairs, 'of times.') # prints out how many times 4 was rolled out of a thousand rolls
print('The total number of trials was', number_of_simulations,'.') # makes sure the program does a thousand trials
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-11-28 22:03:59

假设每只股票都用0- 19 (含)之间的数字表示,这两对是股票0& 1,股票2和3,等等。然后你从0- 19 (包括)中随机选择6个数字,代表丢失的6个股票,并计算出剩余的对数。这样做了很多次(大约100,000次),并找出每一个数字的次数作为剩余的对数。

代码语言:javascript
复制
import random

# variables
number_of_pairs = 10
total_stocks = number_of_pairs * 2
stocks_lost = 6
number_of_simulations = 100_000
prob_of_7_pairs = (1/6)
prob_of_6_pairs = (2/9)
prob_of_5_pairs = (5/18)
prob_of_4_pairs = (1/3)
four_pairs = 0
five_pairs = 0
six_pairs = 0
seven_pairs = 0

# function
for _ in range(number_of_simulations):
    missing_stocks = set(random.sample(range(total_stocks), stocks_lost)) # generate stacks_lost unique random numbers
    possible_pairs = 0
    # loop through the pairs
    for i in range(0, total_stocks, 2):
        # if both stocks of a pair aren't missing, increment possible_pairs
        if i not in missing_stocks and i + 1 not in missing_stocks:
            possible_pairs += 1
    if possible_pairs == 4:
        four_pairs += 1
    elif possible_pairs == 5:
        five_pairs += 1
    elif possible_pairs == 6:
        six_pairs += 1
    elif possible_pairs == 7:
        seven_pairs += 1
    else:
        raise Exception("not supposed to happen")

# results
print(f'There were four possible pairs of socks {four_pairs / number_of_simulations:.2%} of the times.')
print(f'There were five possible pairs of socks {five_pairs / number_of_simulations:.2%} of the times.')
print(f'There were six possible pairs of socks {six_pairs / number_of_simulations:.2%} of the times.')
print(f'There were seven possible pairs of socks {seven_pairs / number_of_simulations:.2%} of the times.')

输出

有4对可能的袜子,34.71%的次数。

有5双可能的袜子,占51.87%。

有6双可能的袜子,占13.11%。

有7双可能的袜子,占0.31%。

我不知道你从哪里得到你的概率,也不知道如何自己计算,但他们似乎错了。

编辑:添加了一个工作示例,请记住我更改了一些变量名。

如果您对此代码有任何疑问,请不要犹豫。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65029562

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档