概率不加到 1
Probability not adding to 1
我想知道是否可以在查看第二个 for
循环时获得一些帮助。在这种情况下,骰子是不公平的,我为偶数分配了很大的权重。代码有两个问题:概率加起来不为 1,奇数的概率大于偶数。
import random
NUM_ROLLS = 100
DIE_SIDES = [1,3,2,4,4,6]
odd_roll_result = 0
even_roll_result = 0
probability = 0
# Create the dictionary to store the results of each roll of the die.
rolls = {}
#Loop for rolling the die NUM_ROLLS times
for r in range(NUM_ROLLS):
roll_result = random.randint(0,len(DIE_SIDES)-1)
if roll_result in rolls:
# Add to the count for this number.
rolls[roll_result] += 1
else:
# Record the first roll result for this number.
rolls[roll_result] = 1
#print(DIE_SIDES[roll_result])
# Print how many times each number was rolled
for roll_result in range(len(DIE_SIDES)):
# print("The number", str(roll_result).format(roll_result, DIE_SIDES[roll_result]),
# "was rolled", str(rolls[roll_result]), "times.")
print("The number", roll_result, DIE_SIDES[roll_result],
"was rolled", str(rolls[roll_result]), "times.")
if roll_result == 1 or roll_result == 3 or roll_result == 5:
odd_roll_result = odd_roll_result + rolls[roll_result]
#print(odd_roll_result)
if roll_result == 2 or roll_result == 4 or roll_result == 6:
even_roll_result = even_roll_result + rolls[roll_result]
#print(even_roll_result)
probability_odd = odd_roll_result/NUM_ROLLS
th_exp = 1/6
prob_th = abs(th_exp-probability_odd)
probability_even = even_roll_result/NUM_ROLLS
print("The probability of getting an odd number is " , probability_odd )
print("Theoretical probability of getting an odd number is " , prob_th )
print("The probability of getting an even number is " , probability_even )
首先,正如其他评论者所指出的,您应该修改 DIE_SIDES 的定义:
DIE_SIDES = [1,2,3,4,5,6]
然后像这样改变掷骰子的线:
roll_result = random.randint(1,len(DIE_SIDES))
甚至更好:
roll_result = random.choice(DIE_SIDES)
您的代码将 0
视为 roll_result,但不将其计入 odd_roll_result
或 even_roll_result
。这就是概率加起来不为 1 的原因。
我想知道是否可以在查看第二个 for
循环时获得一些帮助。在这种情况下,骰子是不公平的,我为偶数分配了很大的权重。代码有两个问题:概率加起来不为 1,奇数的概率大于偶数。
import random
NUM_ROLLS = 100
DIE_SIDES = [1,3,2,4,4,6]
odd_roll_result = 0
even_roll_result = 0
probability = 0
# Create the dictionary to store the results of each roll of the die.
rolls = {}
#Loop for rolling the die NUM_ROLLS times
for r in range(NUM_ROLLS):
roll_result = random.randint(0,len(DIE_SIDES)-1)
if roll_result in rolls:
# Add to the count for this number.
rolls[roll_result] += 1
else:
# Record the first roll result for this number.
rolls[roll_result] = 1
#print(DIE_SIDES[roll_result])
# Print how many times each number was rolled
for roll_result in range(len(DIE_SIDES)):
# print("The number", str(roll_result).format(roll_result, DIE_SIDES[roll_result]),
# "was rolled", str(rolls[roll_result]), "times.")
print("The number", roll_result, DIE_SIDES[roll_result],
"was rolled", str(rolls[roll_result]), "times.")
if roll_result == 1 or roll_result == 3 or roll_result == 5:
odd_roll_result = odd_roll_result + rolls[roll_result]
#print(odd_roll_result)
if roll_result == 2 or roll_result == 4 or roll_result == 6:
even_roll_result = even_roll_result + rolls[roll_result]
#print(even_roll_result)
probability_odd = odd_roll_result/NUM_ROLLS
th_exp = 1/6
prob_th = abs(th_exp-probability_odd)
probability_even = even_roll_result/NUM_ROLLS
print("The probability of getting an odd number is " , probability_odd )
print("Theoretical probability of getting an odd number is " , prob_th )
print("The probability of getting an even number is " , probability_even )
首先,正如其他评论者所指出的,您应该修改 DIE_SIDES 的定义:
DIE_SIDES = [1,2,3,4,5,6]
然后像这样改变掷骰子的线:
roll_result = random.randint(1,len(DIE_SIDES))
甚至更好:
roll_result = random.choice(DIE_SIDES)
您的代码将 0
视为 roll_result,但不将其计入 odd_roll_result
或 even_roll_result
。这就是概率加起来不为 1 的原因。