相对较新的Python,所以任何可怕的编码道歉。在种子内生成随机数
我也想定义,我通过产生做平面上方的半球随机摄像头的位置和角度两个随机数字(在我的例子中,u和v)。
但是,调用py.ops.curve.tree_add函数(生成树)设置某种种子,这意味着我生成的随机数始终相同。
E.g.在示例代码中,根据为basesize/basesplit生成的randint()创建了一系列不同的树。
但是,对于这些生成的每个唯一树,随机数u和v总是相同的。这意味着对于我生成的每个随机树,摄像机角度是特定于该树(并且不是完全随机的)
我假设这是通过某种子发生的,所以我想知道是否有办法告诉python生成随机数并忽略任何种子?
最佳,
实施例的代码:(进口联吡啶是用于混合器蟒API模块)
### libraries
import bpy
from random import random, randint
u = random()
v = random()
obj = bpy.ops.curve.tree_add(bevel = True,
prune = True,
showLeaves = True,
baseSize = randint(1,10)/10,
baseSplits = randint(0,4))
print(u)
print(v)
在情况下,它可以帮助,我的函数来生成的球体放置相机然后指向它(为了简洁起见,我没有在这里包括图书馆/剧本的其余部分 - 它创建了一个围绕一个半径r的定义中心的点并与上述问题分开):
#generate the position of the new camera
def randomSpherePoint(sphere_centre, r, u, v):
theta = 2 * pi * u
phi = acos(2 * v - 1)
x = centre[0] + (r * sin(phi) * cos(theta))
y = centre[1] + (r * sin(phi) * sin(theta))
z = fabs(centre[2] + (r * cos(phi)))
return(x,y,z)
hemisphere_point = randomSpherePoint(centre, radius, u, v)
print(hemisphere_point)
#add a camera at this randomly generated hemispheric location
bpy.ops.object.camera_add(location = hemisphere_point)
the_camera = bpy.data.objects["Camera"]
#rotate the camera to the centre of the plane
camera_direction = centre - camera_location
camera_rotation = camera_direction.to_track_quat('-Z', 'Y')
the_camera.rotation_euler = camera_rotation.to_euler()
代码存在问题:您从'random'导入某些函数,但随后访问'random.uniform'。可以使用'import random'命名空间'random',或者从其中导入'uniform'函数,而不要用'random'作为前缀。 – trincot
啊对不起!它从今天早上的一些修改代码。我一般只使用random()。 编辑示例中的代码以反映此 –