Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate Random Numbers Within A Seed

Relatively new to python so apologies for any terrible coding.

I'm using blender to create random stimuli sets using the sapling add on to create something likeenter image description here

I also want to define a random camera position and angle in a hemisphere above the plane which I do by producing two random numbers (u and v in my example below).

However, calling the py.ops.curve.tree_add function (which generates the tree) sets some kind of seed which means that the random numbers I produce are always the same.

E.g. in the example code it creates a range of different trees depending on the randint() generated for basesize/ basesplit.

However, for each unique tree these generate, the random numbers u and v are always the same. This means for every random tree I generate, the camera angle is specific for that tree (and not completely random)

I assume this occurs via some seed, so I was wondering if there is a way to tell python to generate a random number and ignore any seeds?

Best,

Example code: (import bpy is the python api module for blender)

### 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)

in case it helps, my function to generate a sphere to place the camera and then point it towards the object is (I haven't included the libraries/ rest of the script etc. here for brevity- it creates a point around a defined centre which is a radius r away and works apart from the above issue):

#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()
like image 247
Robert Hickman Avatar asked Oct 20 '25 15:10

Robert Hickman


1 Answers

You can make a instance of random using the class random.Random. An example would be:

randomgen = random.Random()
randomgen.uniform(0,1)

This reason is:

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

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

like image 139
Lasse VB Avatar answered Oct 26 '25 17:10

Lasse VB



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!