如何进行随机 Monte Carlo 扫描并在数据文件中获取输出

How to do a random Monte Carlo scan and getting the outputs in a data file

我有一些 python 程序让我们说 'Test.py' 它有一个 class 并且在 init 里面我必须引入三个随机变量。

import math
class Vector():
def __init__(self,vx,vy,vz):
    self.x=vx
    self.y=vy
    self.z=vz

def norm(self):
    xx=self.x**2
    yy=self.y**2
    zz=self.z**2
    return math.sqrt(xx+yy+zz)

现在我制作了一个 运行 文件 'Testrun.py' 调用这个文件然后为每个数据集生成一个结果

import math
import numpy as np

from Desktop import Test
def random_range(n, min, max):
   return min + np.random.random(n) * (max - min)

model=Test.Vector(x,y,z)

x=random_range(20,2,9)
y=random_range(20,2,9)
z=random_range(20,2,9)

trial_args = np.stack((x, y, z), axis=-1)
for x, y, z in trial_args:
   print(x, y, z, '=>', model.norm())

现在我只想存储给出 'norm'>5 的结果,并想在数据文件中打印输入和输出

保持不变'Test.py'

import math
class Vector():
 def __init__(self,vx,vy,vz):
    self.x=vx
    self.y=vy
    self.z=vz

 def norm(self):
    xx=self.x**2
    yy=self.y**2
    zz=self.z**2
    return math.sqrt(xx+yy+zz)

我们必须将输出条件放在 if 循环中

import math
import numpy as np

from Desktop import Test
def random_range(n, min, max):
return min + np.random.random(n) * (max - min)

x=random_range(20,2,9)
y=random_range(20,2,9)
z=random_range(20,2,9)

trial_args = np.stack((x, y, z), axis=-1)
for x, y, z in trial_args:
    model=Test.Vector(x,y,z)
    if model.norm()>5:
       print(x, y, z, '=>', model.norm())