Skip to Content
Stoatly Docs
Formulas

Random values

Many games run on chance. A sword hits for 3d6, an archer volley lands some of its arrows, a spell rolls twice and keeps the better result. A random formula lets you write that rule down and see every result it can give.

The functions

FunctionWhat it gives
random(min, max)A whole number from min to max, both included
sum_of(count, value)Works out value count times and adds the results
sum_highest(count, keep, value)Works out value count times and adds the highest keep results
sum_lowest(count, keep, value)Works out value count times and adds the lowest keep results
count_at_least(count, value, target)Works out value count times and counts the results that reach target

Random functions use whole numbers. If a field is called which uses a decimal number, round it down using floor() first, such as random(1, floor(value)).

Dice

  • random(1, 6) is one six-sided die.
  • sum_of(3, random(1, 6)) is three six-sided dice added together.
  • sum_highest(2, 1, random(1, 20)) rolls a twenty-sided die twice and keeps the higher result, recreating advantage. Use sum_lowest for disadvantage.
  • count_at_least(5, random(1, 10), 7) is five archers who each hit on a 7 or more. That's a 40% chance each, so the formula gives how many of the five hit.

Anything can go in the count, keep, min and max slots, as long as it works out to a whole number. A weapon's dice field can set the count, so sum_of(dice, random(1, 6)) rolls as many dice as the record says.

Every call rolls fresh

Each random in a formula rolls on its own, every time it is worked out.

  • random(1, 6) * 3 is one result, tripled. The results are 3, 6, 9, 12, 15 or 18.
  • sum_of(3, random(1, 6)) is three separate results added up. The results run from 3 to 18.

The count is worked out once, and the value is worked out again for each result, so each call is independent.

What the numbers must be

Count, keep, min and max must be whole numbers. Use any of these:

  • A whole number written in the formula, such as 3.
  • A number field with the Integer only rule.
  • An Integer parameter.

Anything else is an error, such as "Count must be a whole number." The message says how to fix it: turn on Integer only for the field, or wrap the value in floor().

Some edge cases worth knowing:

  • counts are capped at 1,000.
  • A count of 0 or less gives 0.
  • A keep of 0 gives 0.
  • A keep above the count keeps every result.

See the spread

When a formula has random values, the preview shows the range of results instead of a single number. Pick a record to run it on, or use Test values for a runtime formula.

  • Min, Average and Max show the lowest and highest result and the mean.
  • A sentence such as "Most results fall between 9 and 12." refers to the first and third quartiles.
  • The chart shows the chance of each result. Switch between Exactly, At least and At most to read the chance of rolling exactly 10, 10 or more, or 10 or less.
  • An Estimated badge appears when the formula is too large to count every result. The min and max are calculated automatically and will still be displayed.

If a formula can't be worked, the preview shows no range.

On records

Put a random formula in a formula field and each record shows its range as x - y, such as 3 - 18. As with any Formula field, it is read-only. Sorting by the field uses the lowest value.

In charts

A visual can plot a random formula field. A scatter chart (on the vertical axis) and a bar chart with one record per bar draw a range mark for each record:

  • A line from the lowest result to the highest.
  • A box for the middle 50% of results.
  • A dot for the average.

That lets you compare the distribution of results for each record. Line charts and the horizontal axis of a scatter chart don't offer random fields.

In your game

Random formulas run in your game too, and roll fresh every time you call them. See Formulas in code.

On this page