在种子中生成随机数
Generate Random Numbers Within A Seed
python 相对较新,因此对于任何糟糕的编码表示歉意。
我正在使用搅拌器创建随机刺激集,使用树苗添加创建类似
的东西
我还想在平面上方的半球中定义一个随机相机位置和角度,我通过生成两个随机数(下面示例中的 u 和 v)来实现。
但是,调用 py.ops.curve.tree_add 函数(生成树)会设置某种种子,这意味着我生成的随机数始终相同。
例如在示例代码中,它根据为 basesize/basesplit 生成的 randint() 创建了一系列不同的树。
然而,对于这些生成的每棵独特的树,随机数 u 和 v 总是相同的。这意味着对于我生成的每棵随机树,摄像机角度都是特定于该树的(而不是完全随机的)
我假设这是通过一些种子发生的,所以我想知道是否有办法告诉 python 生成一个随机数并忽略任何种子?
最佳,
示例代码:(导入 bpy 是 blender 的 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()
您可以使用 class random.Random 创建随机实例。
一个例子是:
randomgen = random.Random()
randomgen.uniform(0,1)
这个原因是:
The functions supplied by this module are actually bound methods of a hidden instance of the random.Random class. You can instantiate your own instances of Random to get generators that don’t share state
Python的random module provides the seed()
设置种子的方法
import random
random.seed(12)
random.randint(0,100)
另一种获得树木变化的方法是为树苗插件提供不同的种子。您可以在树比例上方的运算符调整面板中找到它,python API 也接受种子参数。
bpy.ops.curve.tree_add(seed=myseed)
python 相对较新,因此对于任何糟糕的编码表示歉意。
我正在使用搅拌器创建随机刺激集,使用树苗添加创建类似
我还想在平面上方的半球中定义一个随机相机位置和角度,我通过生成两个随机数(下面示例中的 u 和 v)来实现。
但是,调用 py.ops.curve.tree_add 函数(生成树)会设置某种种子,这意味着我生成的随机数始终相同。
例如在示例代码中,它根据为 basesize/basesplit 生成的 randint() 创建了一系列不同的树。
然而,对于这些生成的每棵独特的树,随机数 u 和 v 总是相同的。这意味着对于我生成的每棵随机树,摄像机角度都是特定于该树的(而不是完全随机的)
我假设这是通过一些种子发生的,所以我想知道是否有办法告诉 python 生成一个随机数并忽略任何种子?
最佳,
示例代码:(导入 bpy 是 blender 的 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()
您可以使用 class random.Random 创建随机实例。 一个例子是:
randomgen = random.Random()
randomgen.uniform(0,1)
这个原因是:
The functions supplied by this module are actually bound methods of a hidden instance of the random.Random class. You can instantiate your own instances of Random to get generators that don’t share state
Python的random module provides the seed()
设置种子的方法
import random
random.seed(12)
random.randint(0,100)
另一种获得树木变化的方法是为树苗插件提供不同的种子。您可以在树比例上方的运算符调整面板中找到它,python API 也接受种子参数。
bpy.ops.curve.tree_add(seed=myseed)