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

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

##### 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 on other sites

Thank you anim, merlino and j00ey.

## Create an account

Register a new account