Files
Pasha Stetsenko 2eac6f5aa9 docs: Documentation for built-in functions in Jenny (#2258)
This PR adds missing documentation for all built-in functions available in Jenny.

In addition:

    The bool() function now behaves the same way as the conversion of arguments for an invocation of a user-defined function;
    Consequently, static sets trueValues/falseValues moved from CommandStorage class into the YarnProject;
    Added some tests for several other functions.
2023-01-08 22:17:40 +00:00

1.5 KiB

Random functions

These functions produce random results each time they run.

Internally, each function uses YarnSpinner.random random generator, which can be replaced with a custom generator if you need reproducible draws for debug purposes, or to prevent the player from getting different results upon reload.

dice(n)

Returns a random integer between 1 and n, inclusive. For example, dice(6) will return a random integer from 1 to 6, as if throwing a regular six-sided die.

The argument n must be numeric, and greater or equal than 1. If n is a non-integer, then it will be truncated to an integer value at runtime. Thus, dice(3.5) is equivalent to dice(3).

<<set $roll = dice(6)>>
<<set $coin_flip = if(dice(2) == 1, "H", "T")>>

random()

Returns a random floating-point between 0 and 1.

This function can be used to implement events with a prescribed probability. For example:

<<if random() < 0.001>>
  // This happens only with 0.1% probability
  You found it! The Holy Grail!
<<endif>>

random_range(a, b)

Returns a random integer between a and b inclusive.

Both arguments a and b must be numeric, and they will be truncated to integers upon evaluation. The value of a must be less than or equal to b, or otherwise a runtime exception will be thrown.

The purpose of this function is similar to dice(), but it can be used in situations where a custom range is desired.

<<set $coin_flip = bool(random_range(0, 1))>>