command (MEL) |
Only available in MEL |
rand |
In categories: Language, Math |
Go to: Synopsis. Return value. Related. MEL examples.
rand
[string|int] float|vector [float|vector]
rand is NOT undoable, NOT queryable, and NOT editable.
The command returns random numbers evenly distributed over the
range 0->range or in the case of the second version between
start_range and end_range. The vector version generates an
independent random number for each of the three components.
Multiple random number streams:
The three stream-based variants of rand accept a
stream name parameter which specifies the name of the random number
stream to use when generating random numbers. For details on
random number streams please refer to the section "An overview of
MEL's multiple random number stream support" later in this document.
The behaviour of the
function is identical to the non-stream versions with the exception
that random number generation is performed from the specified rather
than the default random number stream. The first two stream-based
variants take a string which specifies the stream name. The third
variant takes an integer as the stream name (this is a convenience
function which internally converts the integer to a string in the event
the use wants to work with numeric-only stream names).
An overview of MEL's multiple random number stream support:
The random numbers returned by rand, sphrand and
gauss all follow a fixed sequence (called a random
number stream). Prior to the addition of multiple random number
streams to Maya, the only way to ensure repeatability was to
explicitely reseed the generator using the seed function.
There are, however, cases where random calls occur in arbitrary
order and reseeding is either impossible or impractical.
To resolve this issue, MEL supports an arbitrary number of user-defined
random number streams. Each MEL random call (rand, sphrand,
gauss, etc) allows a stream name to be provided which identifies
the random stream to be used during random number generation. Streams are
created implicitely: each MEL random function checks first if the stream
exists and if not, it is created and seeded with an internal default.
The internal default is identical so that the default and user-defined
streams always generate the same sequence of numbers when generated
via an identical set of random calls. (This repeatability can be
avoided by manually reseeding the stream via the seed function).
Stream names may be any user-defined string not containing a "*" as a
character. The stream named "default" is pre-defined and refers to the
standard stream used by the non-stream-based MEL random calls.
The intent with random number streams is that the user can target
specific areas of MEL random calls to use dedicated random number
streams and thus ensure repeatability of the random numbers which
are generated.
Internally, streams are managed through an efficient database,
so it is not unreasonable to allocate large stream counts (e.g.
100,000 streams). Streams may be deleted via the delrandstr
function. The state of a random number stream may be queried via the
randstate function.
float|vector | Command result |
delrandstr, gauss, seed, sphrand
rand 42;
// Result: 39.1327 //
rand 42;
// Result: 23.8585 //
rand -80 42;
// Result: -12.1565 //
rand <<1,1,1>>;
// Result: <<0.531557, 0.920261, 0.515431>> //
rand <<1,1,1>> <<100, 200, 300>>;
// Result: <<81.2325, 38.4956, 266.008>> //
// <b>Multiple random number stream examples:</b><br>
string $s1 = "stream1";
// Result: stream1 //
rand $s1 42;
// Result: 39.1327 //
rand $s1 42;
// Result: 23.8585 //
string $s2 = "stream2";
// Result: s2 //
rand $s2 42;
// Result: 39.1327 //
rand $s2 42;
// Result: 23.8585 //
// Example using the convenience method which allows an integer to be used
// as the stream name:
// Assume `particleID' is an integer... //
rand particleID -80 42;
// Result: 33.6711 //