如何将变量的偏差值合并到我的函数中? [python]
How do I incorporate the bias values from my variables into my function? [python]
我正在模拟这样一个场景:自助洗衣店里有十双袜子,随机丢失了六只袜子。在此之后,至少可以剩下四对,最多七对。我已经编写了一个运行并模拟概率的代码,但是当机会不相等时,它会将所有概率视为均等机会。我的问题是现在如何将偏差合并到此代码中:
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
假设每只股票都用0-19(含)之间的数字表示,配对为股票0和1,股票2和3等。然后您从0-19(含)中随机选择6个数字) 表示丢失的六只股票,并计算剩下多少对。重复多次(大约 100,000 次),找出每个数字出现的次数作为剩余的对数。
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.')
输出
34.71% 的概率有四双袜子。
51.87% 的概率有五双袜子。
13.11% 的概率有六双袜子。
0.31% 的概率有七双袜子。
我不知道你的概率是从哪里来的,也不知道我自己是怎么计算的,但它们似乎是错误的。
编辑: 添加了一个工作示例,请记住我更改了一些变量名称。
如果您对此代码有任何疑问,请随时提问。
我正在模拟这样一个场景:自助洗衣店里有十双袜子,随机丢失了六只袜子。在此之后,至少可以剩下四对,最多七对。我已经编写了一个运行并模拟概率的代码,但是当机会不相等时,它会将所有概率视为均等机会。我的问题是现在如何将偏差合并到此代码中:
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
假设每只股票都用0-19(含)之间的数字表示,配对为股票0和1,股票2和3等。然后您从0-19(含)中随机选择6个数字) 表示丢失的六只股票,并计算剩下多少对。重复多次(大约 100,000 次),找出每个数字出现的次数作为剩余的对数。
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.')
输出
34.71% 的概率有四双袜子。
51.87% 的概率有五双袜子。
13.11% 的概率有六双袜子。
0.31% 的概率有七双袜子。
我不知道你的概率是从哪里来的,也不知道我自己是怎么计算的,但它们似乎是错误的。
编辑: 添加了一个工作示例,请记住我更改了一些变量名称。
如果您对此代码有任何疑问,请随时提问。