Python 随机化方框内的(x、y、z)坐标

Python 随机化方框内的(x、y、z)坐标,python,Python,我对python相当陌生,在我当前的作业中,它研究了3D中的粒子 问题的第一部分要求创建一个程序,将相同的、不重叠的粒子放入立方体晶格中。所以我下面的代码只是迭代所有可能的组合,将其放入XYZ文件中 xyz文件的格式如下: 1000.000000 comment goes here H 0.000000 0.000000 0.000000 H 0.000000 0.000000 1.000000 H 0.000000 0.000000 2.000000 H 0.000000 0.000000 3

我对python相当陌生,在我当前的作业中,它研究了3D中的粒子

问题的第一部分要求创建一个程序,将相同的、不重叠的粒子放入立方体晶格中。所以我下面的代码只是迭代所有可能的组合,将其放入XYZ文件中

xyz文件的格式如下:

1000.000000
comment goes here
H 0.000000 0.000000 0.000000
H 0.000000 0.000000 1.000000
H 0.000000 0.000000 2.000000
H 0.000000 0.000000 3.000000
H 0.000000 0.000000 4.000000
H 0.000000 0.000000 5.000000
H 0.000000 0.000000 6.000000
H 0.000000 0.000000 7.000000
H 0.000000 0.000000 8.000000
下一部分我要做的就是做同样的事情,但是把它们放在一个随机的方式中,限制我拥有的粒子数量。这是我第一部分的代码

import itertools #will help iterating particles 

#we must set the values of diameter and Length
L=10
d=.5

#this is the intial coordinates of the first particle
x,y,z = 0,0,0
counter=0

#The particles will be spread across, the below ensures that there is no overlap of particles
with open("question1.xyz","w") as file:
    file.write("\ncomment goes here\n") #add comments into the 2nd line of the xyz file
    for x,y,z in itertools.product(range(L),repeat = 3):
        file.write('H %f %f %f\n' % (x, y, z))
        counter=counter+1

#this will put the number of particles as the first line
with open("question1.xyz") as infile:
    with open("q1Output.xyz","w") as outfile:
        for i,line in enumerate(infile):
            if i==0:
                outfile.write('%f\n'%counter)
            else:
                outfile.write(line)
我感到困惑的是,我应该如何随机化我的粒子/坐标,并确保没有重叠

有什么想法吗?提前谢谢

**编辑:这就是我到目前为止所遇到的问题,除了要求KT改变L并尽量减少运行程序所需的时间之外——不确定是什么

import random
import time

start_time=time.time() 

text_file=open("textfile.xyz","w")
text_file.write("512\n")
text_file.write("Comment goes here\n")

L=12.5
d=1

#place particles
for partciles in range(0,512)
    proxcheck=1
    while proxcheck !=2 #when the particles has been placed, proxcheck will be set=2, and the program will move onto next
    x,y,z=random.random()*L,random.random()*L,random.random()*L #random numbers for positions
    skipfirsttwolines,proxcheck=0,1;

For line in text_file
    skipfirsttwolines=skipfirsttwolines+1;
    if skipfirsttwolines>2: #in xyz file we dont look at first two lines
        proxcheck=0 #if no overlap particle will be placed
        oldvals=line.split(none)
        oldx=float(oldvals[1])
        oldy=float(oldvals[2])
        oldz=float(oldvals[3])
我不知道该从这里走到哪里,或者我是否应该采取这种方法来确保没有重叠

@大卫:

这就是我想的,有什么建议吗

x,y,z=[],[],[]
for j in range(0,512):
    x.append(0)
    y.append(0)
    z.append(0)
xyz_line = '\n{0} {1} {2} {3}'.format('O',x[0],y[0],z[0])
f.write(xyz_line)

您可以使用代码创建一个随机点

import random

p = (random.randint(0, L), random.randint(0, L), random.randint(0, L))
但如果您需要防止两个点位于同一位置,您可以这样做(将
num_points
设置为您想要的点数后):


这个盒子有多大?(你给出长度
L=10
但直径
d=.5
:这是什么意思?)另外:如果你的位置是整数,你应该将它们格式化为
'H%f%f\n'
:这将给出
h0.0000000.000000 8.000000
(如果数字总是整数,额外的数字会产生误导)L是盒子的长度。因此,它是lxl-感谢您的评论将改变“重叠”对您的程序意味着什么?你能简单地给每个粒子的坐标加上[0,1]中的一个随机值吗?或者如果它们在相邻的晶格单元中,(1,1,0.9)会不会太接近(1,1,1.1)?我想我的教授只是想让它保持整数。所以1.1就不存在了。谢谢,你建议我如何把它放在xyz格式中?我应该设置这样的东西吗?'x,y,z=[],[]对于范围(0512)x.append(0)y.append(0)z.append(0)'xyz_line='\n{0}{1}{2}{3}'。format('O',x[0],y[0],z[0])f.write(xyz_line)@JohnJones:我真的不明白你在那里想做什么。看我的答案——把它写到一个文件是非常简单的。
points = set()
while len(points) < num_points:
    p = (random.randint(0, L), random.randint(0, L), random.randint(0, L))
    if p not in points:
        points.add(p)
for p in points:
    f.write("%s %s %s\n" % p)