python - 在种子中生成随机数

标签 python random blender seed

对于 python 来说相对较新,因此对于任何糟糕的编码表示歉意。

我正在使用 blender 创建随机刺激集,使用树苗添加创建类似 enter image description here 的东西

我还想通过生成两个随机数(在我的示例中为 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()

最佳答案

您可以使用 random.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

(来自 https://docs.python.org/3/library/random.html)

关于python - 在种子中生成随机数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43201887/

相关文章:

Python Flask after_request 和 before_request 用于一组特定的请求

python - 在 Python 中测试抽象类

php跟踪随机条件

python - 通过 Blender python 渲染和保存图像

python - 问题: how to reference to objects/variables created in decorator from injected method?

python sys.exit 无法正常工作

mysql - 来自 Sql 数据库的简单随机样本

c# - Random().Next() 流需要多长时间才能重复?

python - 如何通过 Python API 在 Blender 2.50 中创建一个简单的网格

performance - 我应该在将模型导入 Unity 之前在 Blender 中烘焙模型吗?