Wiki

trigger · set_temp_variable_to_random

Definition

  • Supported scope:any
  • Supported target:none

Description

sets a temp variable to a random value. example 
set_temp_variable_to_random = num_dogs #sets num_dogs a random value between [0, 1) 
set_temp_variable_to_random = { 
	var = num_dogs #variable to set 
	min = 5 #default 0. value will be set in between [min, max) 
	max = 10 #default 1. value will be set in between [min, max) 
	integer = yes #default no. if yes the number value will be an integer 
}

Hands-On Notes

Hands-on notes are AI-generated and checked against the vanilla command vocabulary — treat them as a starting point, not authoritative reference. The definition above is the game's own documentation.

Hands-On Usage

set_temp_variable_to_random as an effect is commonly used to generate a random number before an if conditional trigger, then validate probability using check_variable. This pattern is ideal for implementing custom random event probability systems, randomized reward/penalty values, or procedural map generation logic. For example, when simulating random supply amounts or randomized technology bonuses in a mod, generate the random value first, then compare it in a trigger:

# Assign value in effect block first
set_temp_variable_to_random = {
    var = roll_value
    min = 0
    max = 100
    integer = yes
}
# Then check in trigger/if
if = {
    limit = {
        check_variable = { roll_value < 30 }
    }
    # Low probability branch logic
}

Synergy

  • [check_variable](/wiki/trigger/check_variable): After generating a random temporary variable, you almost always need to pair it with check_variable to compare whether the random value falls within a target range, thus implementing probability gating.
  • [add_to_temp_variable](/wiki/effect/add_to_temp_variable): Add a fixed offset to the random value (such as difficulty bonuses), then perform subsequent checks or assignments.
  • [clamp_temp_variable](/wiki/effect/clamp_temp_variable): After generating the random result, use clamp_temp_variable to constrain it within a safe range, preventing extreme values from breaking game balance.
  • [multiply_temp_variable](/wiki/effect/multiply_temp_variable): Scale the random value (such as multiplying by a country's industrial coefficient) to convert the raw random number into a meaningful final value.

Common Pitfalls

  1. Using it as a trigger: set_temp_variable_to_random is fundamentally an effect and cannot be written directly inside a pure trigger block (such as limit = { ... }). The correct approach is to execute the assignment in an outer effect block first, then enter an if branch with limit to perform the check.
  2. Overlooking the half-open interval behavior: The random range is [min, max). If you need to include the max value exactly (like a dice roll 1-6 including 6), set max to the target maximum plus 1; otherwise the maximum value can never be rolled.