Random number for one frame

I’m doing a driver, let’s say for the location in X. I want to generate a random number but only once. I don’t want it to change at every frame. What would be the syntax?

Random can have a seed, that will ensure that the random sequence will stay the same.

import random
random.seed(42)
x =random.random()

What I mean is that the random number changes every frame, which is not what I want. I want a number to be generated at frame 1 and stay the same for the entire sequence

(Who is faster?) x is just a name, read:

oneRandomNumber = random.random()

(Edit: inserted a comma)

Not sure if I get you correct, so you can also use noise.noise((x,y,z)) if that suits your case better.

No i think you got it: At the first frame get a random number which wouldn’t chane.
Does @BlenderBob do programming ? The x here has nothing to do with:

1 Like

X here is only the axis, not a variable.

Yes… (what’s the saying? we get our wires crossed?)
And what @Debuk posted is the solution (?) Getting one random number and remembering this for the rest of … oh wait … drivers are executed for ever frame… you wanna preserve the value, has to be more than a custom function, (Programmer thinking) are classes allowed… no? No. hmm the driver has to be connected to something what is executed only once at startup and additionally queried in a driver expression connected to the x location… i believe that’s not so easy … maybe an custom property because for the driver ther is Add Input Variable which can be a Single Property with given Path Data/RNA (internal blender data format) … but where is ??? :crazy_face:

The documentation of blender is sometimes … easy if you know what you done in the original source code … but i didn’t do nothing of it… But all of that doesn’t help you :crying_cat_face: :broken_heart:

(By the way: Thanks for your YT-channel, if i would be registered on YT i would subscribe)

The question is: why would u want that?
It would give u a different value every time u start the animation. This isvnondeterminism. Chaos par excellence !?

Well, I think there’s a misunderstanding here. Perhaps I shouldnt have used x as variable in my script to prevent confusion with your location x. It wasnt meant to be the same. Overall it would have been easier to help you if you’d have shown what you have so far. I am still not sure when you want your randomness to happen, just that you dont want it during framechanges. Anyhow, by using a specific location for the noise function you can use the result as a random value between 0 and 1. You can multiply that if you need bigger values I controlled it with an empty here. So moving that generates a new random value, otherwise its fixed and not changing every frame.

I hope that helps.

Sorry guys, I had a crazy day. Didn’t get a chance to follow everything.

This is the goal. I instance the same character many times in the scene. Let’s say it’s a guy running. I don’t want all the guys to run in perfect sync so I would like to generate a random number, with a seed so it stays the same every time I reopen the file, and that number would be applied to the offset of the alembic.

In an ideal world, I would like to do this with geo nodes but I don’t think there’s a way to offset on a per character basis. Geo nodes will just instance everything the same.

Well late here meanwhile.

But this should roughly be what you want. Add this as a script in the scripting editor and run it.

import bpy
import random
global result
result = 0

def rnd(input):

    global result
    if input == 1: 
        result = random.random()
    return result

bpy.app.driver_namespace["rnd"] = rnd

Add this to your driver expression: rnd(frame)

Start the animation

Edit: Heres an example with random seed and identical results.

import bpy
import random


def rnd(self,input):
    
    random.seed(self.name)
    result = random.random()*6.283
    return result
 


bpy.app.driver_namespace["rnd"] = rnd

Used like that: rnd(self,frame)

2 Likes

love your youtube content!

this expression below works for me to generate frame playback with a per instance random offset, and is one friendly line:

‘use self’ allows each instance to generate a random offset unique to that instance. in this case i hashed the object instance’s name (which initially generates an ungodly large random number).

then taking the modulus of it, i can turn it into a useful range.

so in my example, the ‘%10’ means it will return an integer between 0 and 9. ‘%50’ would be 0 and 49, and so on.

lastly you could further manipulate it to include decimal points with a final division, ie:

hash(self.name)%10/10 would give you a single decimal number between 0.0 and 0.9
hash(self.name)%1000/100 would give you a two decimal number between 0.00 and 9.99

one caveat: i have no idea how slow a ‘slow python expression’ is! doesn’t seem to immediately cause any slow downs, but i didn’t stress test this on hundreds of objects or anything!

Thanks!

It does generate a random number but it’s still changing every frame.

Yep! That worked! Thanks!

EDIT: It doesn’t work anymore when I reopen the file, even if I run the script.

1 Like

ah, i was getting ahead of myself trying it for explicitly setting the animation frame with an offset, if you just want a single, unchanging random number, remove the ‘frame +’ from the expression

Ok, let’s try again. Maybe I wasn’t clear enough. Attached is a file with three cubes in it. The cubes are linked to the alembic cache. They all bounce at the same time because they use the same cache. I’m trying to randomly change the offset, per object, so that they don’t all move in sync. :slight_smile:

bouncinfCubes.zip (100.1 KB)

Thats somehow caused by the activation order. The method used in the expression isnt already there cause the script hasnt run yet.

@BlenderBob Best is to save the script as eg “random.py” in the same folder as your blendfiles and set in the “text” menu of the scripting editor the “register” checkbox. Then the method is there at start. Save your file. Everything should work then directly after opening the file.

1 Like

@BlenderBob Btw. I saw you used the script without any changes. The random value in my script was multiplied with 6.283 to get a value range from 0 to 2Pi for rotations. As you dont use it for rotations you may want another value range you should change that value. :slightly_smiling_face:

(So once again discord didn’t tell me there are new post,anyway)

But for instancing the same animated character on different locations and timeoffset… there was something… found it (from 2015) : How to animate a battle in Blender.