如何在python中分配随机浮点数

时间:2013-09-13 16:32:51

标签: python math numpy

我使用此代码生成xyz坐标。

from random import *

uniqcoord = [7.63, 28.05, 66.36]         # my file contains 100 such list of points

for i in range(10):
    i = i + 1
    x,y,z = uniqcoord
    x1,y1,z1 = (uniform(x[0]-3.5,x[0]+3.5), uniform(y[1]-3.5,y[1]+3.5), uniform(z[2]-3.5,z[2]+3.5))
    print i, '\t', x1,y1,z1

当我运行此程序时显示错误。
当我使用孔编号运行此程序时,它可以工作 如何解决????

2 个答案:

答案 0 :(得分:3)

您的xyzfloat,而非list。所以你不能拥有x[0]等等。您将获得no attributetypeerror

答案 1 :(得分:1)

您在x1,y1,z1行中错误地使用了x,y,z。看看我在下面做了什么;

from random import *

uniqcoord = [7.63, 28.05, 66.36]         # my file contains 100 such list of points

for i in range(10):
    i=i+1
    x,y,z = uniqcoord
    x1,y1,z1 = (uniform(uniqcoord[0]-3.5,uniqcoord[0]+3.5), uniform(uniqcoord[1]-3.5,uniqcoord[1]+3.5), uniform(uniqcoord[2]-3.5,uniqcoord[2]+3.5))
    print i, '\t', x1,y1,z1

x [0]不存在,但是uniqcoord [0] = x因为uniqcoord = [x,y,z],依此类推。

结果:

1   5.86941266341 29.4004245806 67.1323961576
2   6.38143060206 29.7045813689 69.4867869245
3   5.55280335095 29.9472835241 63.7388152633
4   10.5607637875 26.6269381673 69.5256503601
5   7.29826364813 28.5740308696 65.2122276564
6   8.24134391937 30.880058802 69.8445734597
7   10.246919304 27.9240839326 64.9480054046
8   8.26957559527 28.5700768795 63.996117793
9   5.88677020227 30.0621250245 63.7431176092
10  8.98100830174 27.3378753286 63.1329446911

认为这就是你要找的东西。