Skip to content Skip to sidebar Skip to footer

Randomizing (x,y,z) Coordinates Within A Box

I am fairly new to python and in my current assignment it looked at particles in 3D. The first part of the question asked to create a program that put identical, non overlapping p

Solution 1:

You can create a random point with the code

import random

p = (random.randint(0, L), random.randint(0, L), random.randint(0, L))

But if you need to prevent having two in the same place, you can do (after setting num_points to the number of points you want):

points = set()
whilelen(points) < num_points:
    p = (random.randint(0, L), random.randint(0, L), random.randint(0, L))
    if p notin points:
        points.add(p)

To write the resulting points to a file:

for p in points:
    f.write("%s %s %s\n" % p)

Post a Comment for "Randomizing (x,y,z) Coordinates Within A Box"