Skip to main content

$reactions.random

This method returns a random integer number in the specified range.

Syntax

The method accepts one argument: an integer max value:

$reactions.random(10);

The method returns a random integer from 0 to max (not including max).

Usage details

  • The method return values can be overridden in XML tests using the <random> tag. This ensures that the tests run stably.

    caution
    You cannot redefine the return values of the Math.random built-in JavaScript method. Using it in Tovie Platform scripts is not recommended.
  • The return values can be overridden in the script by modifying the $request.data.smartRandom object.

  • All return values are stored in $response and can be used to re-execute the script with the same results.

  • The method ensures that the returned random numbers are not repeated more often once per every max / 2 consecutive method calls.

How to use

Consider an example state which emulates a dice game and prints a sum of random numbers from 1 to 6:

state: RollDice
intent!: /RollDice
# Generate two random numbers from 1 to 6.
script:
$temp.diceOne = $reactions.random(6) + 1;
$temp.diceTwo = $reactions.random(6) + 1;
# Use string substitutions to form a response.
a: {{$temp.diceOne}} and {{$temp.diceTwo}}, {{$temp.diceOne + $temp.diceTwo}} in total.