Note that this member function replaces the current value of a variable with a random
value. So, if you want to set the value of a variable X to a random value,
you would use code similar to the following:
ln X;
X.Random( 100 )
...
cout << X << endl;
|
There are times when you do not want to replace the current value of a variable, for
instance, if the random number is part of an expression. In this case, you can use a
temporary ln object to generate the random value. This would look similar
to the following:
ln X = ln( ).Random( 100 ) % M;
|
In the above example, we are creating a random number mod M. This could
have been done with several lines of code, first creating X, then assigning it a
random value, and finally reducing it mod M. In the example above, however,
we do this in one line of code by using a temporary (unnamed) ln object. In
C++, you can use a constructor anywhere you would otherwise use a variable. This
results in a temporary object being created, used, then destroyed. Note that this is
not always an efficient process (it is in this example, though).