[英]Monte Carlo Python Question: Simulation of three dice in which the sum > 10, returns True, otherwise return False
This is a Monte Carlo Simulation question.这是一个蒙特卡洛模拟问题。 Here is my code.
这是我的代码。
def simulate():
"""
Simulate three dice and return true if sum is > 10
"""
die_1 = randint(1,6)
die_2 = randint(1, 6)
die_3 = randint(1,6)
sum = die_1 + die_2 + die_3
if sum > 10:
return True
else:
return False
The next steps are to use a for loop and call the simulate function to sum up the number of results that end up as True.接下来的步骤是使用 for 循环并调用模拟 function 来总结最终为 True 的结果数。 My idea of doing this:
我这样做的想法:
true_results = 0
for trial in range(1000):
true_results += simulate()
However, how would I determine what is a True or False result?但是,我如何确定什么是 True 或 False 结果? And does this code make sense?
这段代码有意义吗?
For checking, you can store all summation result to see whether the occurrence, and probability roughly match your expectation.为了检查,您可以存储所有求和结果,以查看发生的次数和概率是否与您的预期大致相符。
Your code is fine if it is error-free although the indentation is weird.尽管缩进很奇怪,但如果没有错误,您的代码就没问题。 And i suggest not to use 'sum' as a variable name as it is a build in function name.
我建议不要使用“sum”作为变量名,因为它是在 function 名称中构建的。 It still works, but this may get u into trouble in the future.
它仍然有效,但这可能会给你带来麻烦。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.