0

I have a python program which takes random numbers as input and give a number of separate outputs. The run file is from This answerThe norm of vector. So how to modify the run file to get each new (x,y,z) output in seperate data files like 1.txt,2.txt....20.txt

user105697
  • 39
  • 2
  • 9

1 Answers1

1

Keeping the same '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)

Add a counter to keep track of the valid output file to be created

import math
import numpy as np

from Desktop import Test

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

file_count = 1
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:
        ifp = open(str(file_count) + ".txt", "w")
        ifp.write("{}, {}, {} => {}".format(x, y, z, model.norm()))
        ifp.close()
        file_count += 1
ranka47
  • 995
  • 8
  • 25