Python 蒙特卡罗飞镖模拟器

Python 蒙特卡罗飞镖模拟器,python,python-3.x,montecarlo,Python,Python 3.x,Montecarlo,我一直在尝试使用Python3中的蒙特卡罗模拟制作一个dart模拟器。到目前为止,我已经编写了以下代码: import random import math n = (input("Enter the number of darts you have. ")) count = 1 circleval = 0 squareval = 0 while count <= n: y = 2*random.random()*2-1 x = 2*random.random()*2-1

我一直在尝试使用Python3中的蒙特卡罗模拟制作一个dart模拟器。到目前为止,我已经编写了以下代码:

import random
import math

n = (input("Enter the number of darts you have. "))
count = 1
circleval = 0
squareval = 0
while count <= n:
    y = 2*random.random()*2-1
    x = 2*random.random()*2-1
    if math.sqrt((x-1)**2+(y-1)**2)<=1:
        circleval+=1
        squareval+=1
    else:
        squareval+=1
    count+=1

print("Pi is " + 4*(1.*circleval/squareval))
随机导入
输入数学
n=(输入(“输入您拥有的省道数”)
计数=1
圆周=0
平方值=0
而计数有两个问题:

n = input("Enter the number of darts you have. "))
应该是:

n = int(input("Enter the number of darts you have. "))
print("Pi is " + str(4*(1.*circleval/squareval)))
(因为您希望将
n
视为一个整数)

应该是:

n = int(input("Enter the number of darts you have. "))
print("Pi is " + str(4*(1.*circleval/squareval)))
因为你不能给一个数字加一个字符串

除此之外,我不确定计算是否正确,但这将是另一个问题。

两个问题:

n = input("Enter the number of darts you have. "))
应该是:

n = int(input("Enter the number of darts you have. "))
print("Pi is " + str(4*(1.*circleval/squareval)))
(因为您希望将
n
视为一个整数)

应该是:

n = int(input("Enter the number of darts you have. "))
print("Pi is " + str(4*(1.*circleval/squareval)))
因为你不能给一个数字加一个字符串


除此之外,我不确定计算是否正确,但这将是另一个问题。

这不起作用的主要原因是:

n = (input("Enter the number of darts you have. "))
将字符串放入
n
,我们可以通过以下方法解决此问题:

n = int(input("Enter the number of darts you have. "))
print("Pi is " + str(4*(1.*circleval/squareval)))
需要一个字符串,但您没有提供,我们可以通过以下方法解决此问题:

n = int(input("Enter the number of darts you have. "))
print("Pi is " + str(4*(1.*circleval/squareval)))

对于
n=100000
,这给了我
3.14368
(尽管在几个模拟中可能会有所不同)。

这不起作用的主要原因是:

n = (input("Enter the number of darts you have. "))
将字符串放入
n
,我们可以通过以下方法解决此问题:

n = int(input("Enter the number of darts you have. "))
print("Pi is " + str(4*(1.*circleval/squareval)))
需要一个字符串,但您没有提供,我们可以通过以下方法解决此问题:

n = int(input("Enter the number of darts you have. "))
print("Pi is " + str(4*(1.*circleval/squareval)))

对于
n=100000
,这给了我
3.14368
(虽然在几个模拟中可能会有所不同)。

除了已经确定的字符串到整数的问题,如果将
x
y
边界设置为
[-1,1]
,您可能会发现这更简单 此外,考虑使用NUMPY:

import numpy as np

n = int(input("Enter the number of darts you have. "))
count = 1
circleval = 0
squareval = 0
while count <= n:
    y = np.random.uniform(low=-1, high=1)
    x = np.random.uniform(low=-1, high=1)
    if np.sqrt(x**2 + y**2) <= 1:
        circleval+=1
        squareval+=1
    else:
        squareval+=1
    count+=1

print("Pi is", 4*(1.*circleval/squareval))
注:
-您不需要跟踪
squareval
,只需使用
n

-您可以使用Numpy的矢量化操作跳过
,而
-循环:

area = 4
n = int(input("Enter the number of darts you have. "))

X = np.random.uniform(low=-1, high=1, size=n)  
Y = np.random.uniform(low=-1, high=1, size=n)   

dist = np.sqrt(X**2+Y**2);  

in_circle = np.sum(dist < 1.0)

print("Pi is", area * in_circle/n)
面积=4
n=int(输入(“输入您拥有的省道数”)
X=np.random.uniform(低=1,高=1,大小=n)
Y=np.random.uniform(低=1,高=1,大小=n)
距离=np.sqrt(X**2+Y**2);
圆内=np.和(距离<1.0)
打印(“圆周率”,面积*在圆圈内/n)

除了已经确定的字符串到整数问题外,如果将
x
y
边界设置为
[-1,1]

此外,考虑使用NUMPY:

import numpy as np

n = int(input("Enter the number of darts you have. "))
count = 1
circleval = 0
squareval = 0
while count <= n:
    y = np.random.uniform(low=-1, high=1)
    x = np.random.uniform(low=-1, high=1)
    if np.sqrt(x**2 + y**2) <= 1:
        circleval+=1
        squareval+=1
    else:
        squareval+=1
    count+=1

print("Pi is", 4*(1.*circleval/squareval))
注:
-您不需要跟踪
squareval
,只需使用
n

-您可以使用Numpy的矢量化操作跳过
,而
-循环:

area = 4
n = int(input("Enter the number of darts you have. "))

X = np.random.uniform(low=-1, high=1, size=n)  
Y = np.random.uniform(low=-1, high=1, size=n)   

dist = np.sqrt(X**2+Y**2);  

in_circle = np.sum(dist < 1.0)

print("Pi is", area * in_circle/n)
面积=4
n=int(输入(“输入您拥有的省道数”)
X=np.random.uniform(低=1,高=1,大小=n)
Y=np.random.uniform(低=1,高=1,大小=n)
距离=np.sqrt(X**2+Y**2);
圆内=np.和(距离<1.0)
打印(“圆周率”,面积*在圆圈内/n)

您遇到了什么错误?
n=(输入(“输入您拥有的省道数”)
给您一个字符串,您可能想将其转换为
int
,例如:
n=int(输入(“输入您拥有的省道数”)
TypeError:“执行此操作后,将其转换为int,我得到此错误。打印文件“Untitled 12.py”,第18行(“Pi是”+4*(1.*circleval/squareval))类型错误:必须是str,而不是float您遇到了什么错误?
n=(输入(“输入您拥有的省道数”)
给您一个字符串,您可能想转换为
int
,例如:
n=int(输入(“输入您拥有的省道数”)
TypeError:'在我这样做之后,将其转换为int,我得到这个错误。文件“Untitled 12.py”,第18行,打印(“Pi是”+4*(1.*circleval/squareval))类型错误:必须是str,而不是float谢谢,我会尝试将其输入原始代码。它工作得很好,谢谢您的帮助。尽管听起来很愚蠢,但在提问之前,他一直试图将字符串放在print语句之后。谢谢你的修复。谢谢你,我会尝试把它输入到原始代码中。效果很好,谢谢你的帮助。尽管听起来很愚蠢,但在提问之前,他一直试图将字符串放在print语句之后。感谢您对alfasin的修复。@Blake McLaughlin这应该是公认的答案,因为Willem也在解释算法实现的问题。是的,我的错,我刚刚检查过,我一直在尝试将变量n更改为for循环,以便我可以获得pi等于的长范围输出。很抱歉没有马上给你评分,那是我的错,谢谢你的更正@Blake McLaughlin这应该是公认的答案,因为Willem也在解释算法实现的问题。是的,我的错,我刚刚检查过,我一直在尝试将变量n改为for循环,这样我就可以得到pi等于的长范围输出。很抱歉没有马上给你评分,那是我的错,谢谢你的更正!