Random (Rnd)

What is it?

The Rnd function produces a pseudorandom number that is greater than 0 and less than 1. It requires no arguments.

What is the Syntax?

The Rnd function’s syntax is as follows:

Rnd();

Why might I want to use this?

Simply generating a random number between 0 and 1 on its own may not have a great deal of use to you, but combining this generated number with arithmetic functions allows you to produce much more complex random numbers, which you can then use to introduce probability and chance into your expressions.

For example, if we wanted to create an expression that models the rolling of two six-sided dice, we first need to combine the Random function with the multiplication operator to multiply our random number by the number 6:

Rnd() * 6;

The possible results of this expression are 0 > result < 6, including all decimal values in between. Since the value of a dice roll must be an integer (1, 2, 3, 4, 5, 6), we need to convert the decimal results of our Rnd function into the nearest non-zero integer number. For this, we need to use the Ceiling function:

Ceil( Rnd() * 6 );

This expression returns an integer value between 1 and 6, which satisfies a single die roll. To complete the expression and fully model the score of two random dice being rolled, we create a duplicate of the first expression and join it to the first using the Sum function:

Ceil( Rnd() * 6 ) + Ceil( Rnd() * 6 ) := TwoDiceScore;

 

<< Previous: Floor | Next: Lesson Summary >>