Jump to content
ChazS

Understanding rand()

Recommended Posts

I'm trying to understand how to properly write/understand rand() functions. In particular, when emitting a particle from a random position along the x-axis, within a specific range (e.g. 2.5 to 2.9 units).  My best guess is to write the following expression for the x-axis of the POP location node: fit01(rand($F), 2.5, 2.9))  but the particle launches from the same point every time since the starting frame is always the same. However, I don't know any other way to seed the start point to give it a sense of randomness (real, pseudo, or otherwise).

Obviously I'm misunderstanding something here so if anyone can point me toward my mistake...

Many thanks in advance.

 

Share this post


Link to post
Share on other sites
1 hour ago, ChazS said:

...x-axis of the POP location node: fit01(rand($F), 2.5, 2.9))  but the particle launches from the same point every time since the starting frame is always the same...

you have mismatched parentheses, try this:

fit01(rand($F), 2.5, 2.9)

 

Share this post


Link to post
Share on other sites

Oops, my bad, but the typo was only here and not in the actual file. X-pos updates every frame (which makes sense given how this is written) but there isn't any randomization on the first frame, so it always starts in the same spot.

How do I get the first frame to randomize?

Share this post


Link to post
Share on other sites

Well, the particle "launches from the same point every time since the starting frame is always the same" it's true, as rand is a pseudo-random function. If you need some variation you'll need to create some sort of seed, so you can put $F+n (with n as any number, like $F+354 or $F+387.24) or $F*n. You can also add a parameter and use it as seed, so you just change that value with a slider and not hard coding it.

EDIT: it should be something like fit01(rand($F+354),2.3,2.9) and you can change 354 (n) as you want

Edited by merlino
Adding the expression example

Share this post


Link to post
Share on other sites

It's not usually desirable to have a random number that changes every time it's evaluated, in general you want the same result every time you feed the function the same seed [in your case $F].

What I usually do is as merlino suggests, but I usually add 'seed', 'min' & 'max' parameters to the node [or a control null somewhere] and reference them, eg :

fit01(rand($F + ch("seed")), ch("min"), ch("max"))

If you do really want a different result every time you could write a python expression in the seed field to, for example, return the time.

Edited by j00ey

Share this post


Link to post
Share on other sites

Thank you anim, merlino and j00ey.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×