Reproduce randomness
 
 
 

If you execute the rand, sphrand, and gauss functions repeatedly in an expression, Maya returns a sequence of random numbers. (See Random number functions for details on these functions.) Each time you rewind and play your animation, the sequence of random numbers is different. Often, you’ll want to generate a sequence of random numbers that repeats each time your animation plays.

For instance, suppose you use the rand function to assign a random radius to each particle in a stream of emitted particles rendered as Spheres. By default, Maya gives the particles a different sequence of random radius values each time your animation plays.

To create the same radius values each time the animation plays, you can use the seed function in an expression before the rand, sphrand, or gauss functions execute. There’s no need to execute the seed function more than once per animation unless you need to generate several different repeating sequences of random numbers as your animation plays.

ImportantWhen you set a seed value in an expression or MEL script, the seed value affects the rand, sphrand, and gauss functions in other expressions and MEL scripts. Such functions are affected by this seed value in all scenes you open subsequently in the current work session.

This seed value is unrelated to the Seed attribute available in the particle shape node. The seed function therefore doesn’t affect randomness created with dynamics.

Example

Suppose you use the rand function to position several marbles at random translateX positions in your scene at frame 1:

if (frame == 1)
{
	marble1.tx = rand(-10,10);
	marble2.tx = rand(-10,10);
	marble3.tx = rand(-10,10);
	marble4.tx = rand(-10,10);
}

The rand(-10,10) returns a random number between -10 and 10 each time it executes. When you rewind the animation to frame 1, Maya might assign these values to the translateX attributes of the marbles:

Attribute Value
marble1.tx 2.922
marble2.tx 5.963
marble3.tx -4.819
marble4.tx 7.186

The next time you rewind the animation to frame 1, each marble’s translateX attribute receives a different random value. Maya might assign these values:

Attribute Value
marble1.tx -3.972
marble2.tx 9.108
marble3.tx -7.244
marble4.tx -3.065

You might prefer the marbles’ translateX values to stay the same when you rewind, for instance, so you can composite the marbles correctly among a foggy backdrop.

You can use the seed function to keep the sequence of random values returned by the rand function consistent when you rewind the animation.

if (frame == 1)
{
	seed(10);
	marble1.tx = rand(-10,10);
	marble2.tx = rand(-10,10);
	marble3.tx = rand(-10,10);
	marble4.tx = rand(-10,10);
}

By setting the seed value to an arbitrary number, for instance, 10, the subsequent executions of the rand function return a repeating sequence of random numbers.

When you rewind the animation the first time, Maya might assign these values to the translateX attributes of the marbles:

Attribute Value
marble1.tx 8.020
marble2.tx -2.973
marble3.tx -7.709
marble4.tx 0.741

Each time you rewind the animation thereafter, Maya assigns these same values to the translateX attributes of the marbles. The marbles don’t move.

Each time a statement sets the seed value to 10, the subsequent executions of the rand function return numbers from the sequence starting at the beginning number. In other words, resetting the seed value to 10 restarts the random number generation process to the first value in the sequence.

Suppose you alter the expression to this:

if (frame == 1)
{
	seed(10);
}
	marble1.tx = rand(-10,10);
	marble2.tx = rand(-10,10);
	marble3.tx = rand(-10,10);
	marble4.tx = rand(-10,10);

When you rewind the animation to frame 1, the expression sets the seed to 10. Maya assigns values to the marbles’ translateX attributes as in the previous expression.

Because the expression doesn’t set the seed value in frames other than frame 1, playing the animation causes the rand function to return a new, yet repeating, sequence of random numbers each frame. If you play the animation several times, the translateX values will constantly change during animation, but the sequence of values will be identical each time you play the animation.

You can assign the seed a different value to generate a different sequence of returned values. See seed for details.