Randomness

function description example
rand( )

Returns a random floating point number or vector within a range of your choice.

float rand(float maxnumber)

float rand(float minnumber, float maxnumber)

vector rand(vector maxvector)

vector rand(vector minvector, vector maxvector)

To control the random values returned by this function, see seed.

translateY = rand(5);

rand( ) results in a very jagged curve; every frame has an entirely different value from the last.

rand( ) can be used effectively for "jitter" by using a small range.

Another good use for rand( ) might be when selecting a number periodically, such as in a conditional statement. In the example below, the variable $xDir is given a new value every 60 frames of the animation:

if ( frame % 60 == 0) $xDir = rand( 5 );

noise( )

Returns a random number from -1 to 1 according to a Perlin noise field generator.

float noise(float number)

float noise(float xnum, float ynum)

float noise(vector vector)

If you execute this function with the same argument value repeatedly, the function returns the same random value each time it executes.

If you execute this function with an argument value that steadily increases or decreases in fine increments over time, the function returns random values that increase and decrease over time.

translateY = noise(time);

noise( ) provides a smoother random motion over time, but you have to use a steadily increasing value, in this case the predefined variable "time".

noise( ) always returns a value between -1 and 1, so you should multiple the result and/or add an offset to get the range you need:

translateY = (noise(time) * 5 ) + 5;

sphrand( ) Returns a random vector value that exists within a spherical or ellipsoidal region of your choice. An ellipsoid is a sphere scaled along its X-, Y- or Z-axes.

vector sphrand(float radius)

vector sphrand(vector vector)

radius is the radius of a sphere in which the returned vector exists.

vector is the radius of an ellipsoid along the X-, Y-, and Z-axis.

To control the random values returned by this function, see seed.

vector $randomVect = sphrand(1);
translateX = $randomVect.x;
translateY = $randomVect.y;
translateZ = $randomVect.z;


sphrand( ) outputs a vector. The expression above shows how you can set each translation attribute to a component of a vector.

Note this function has the characteristic of result values defined in a spherical region, whereas using 3 instances of rand( ) would give results in a "cubic" region:

translateX = rand(-1,1);
translateY = rand(-1,1);
translateZ = rand(-1,1);

gauss( ) Returns a random floating point number or vector. The number returned falls within a Gaussian (bell curve) distribution with mean value 0.

float gauss(float stdDev)

vector gauss(float XstdDev, float YstdDev)

vector gauss(vector stdDevVector)

stdDev specifies the value at which one standard deviation occurs along the distribution. This gives a one-dimensional Gaussian distribution.

XstdDev and YstdDev specify the values for one standard deviation. This gives a two-dimensional Gaussian distribution in the XY plane. The right component of the vector returned is 0.

stdDevVector specifies the vector component values for one standard deviation. This gives a three-dimensional distribution.

To control the random values returned by this function, see seed.

translateY = gauss(1);

note the results fall outside the given value of 5 (-5 to 5) because the value determines the "standard deviation" range defining the bell curve:

seed( ) Sets a seed value the gauss, rand, and sphrand functions use to generate random numbers. If you assign a value to the seed then execute the gauss, rand, or sphrand function repeatedly, an identical sequence of random numbers is generated.

int seed(int number)

if (frame == 1) seed(1);
translateY = rand(5);


Every frame the second line is executed, including when the animation rewinds to frame 1 again. The first line is a conditional statement that explicitly sets the random seed to 1, thereby reproducing the exact same random stream of numbers each time the animation is played.

 

These are just a few functions available to use in expressions or MEL scripts. Find more math functions by using the Maya online docs menu: Help > Mel Command Reference.