Wiki

effect · randomize_variable

Definition

  • Supported scope:any
  • Supported target:none

Description

refer to randomize_temp_variable

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

randomize_variable behaves identically to randomize_temp_variable and is used to assign a random numerical value to an already-declared persistent variable (not a temporary one). It is commonly used in scenarios requiring the random result to persist, such as generating a one-time random event weight or random resource stockpile for a country. The key difference from the temporary variable version is that the result is saved on the scope for subsequent logic to read repeatedly.

# Generate a random stockpile value for the current country and store it in a persistent variable
country_event = {
    immediate = {
        set_variable = { var = resource_roll value = 0 }
        randomize_variable = {
            var = resource_roll
            min = 1
            max = 100
        }
        if = {
            limit = { check_variable = { var = resource_roll value > 80 } }
            add_victory_points = { province = 1234 value = 5 }
        }
    }
}

Synergy

  • [set_variable](/wiki/effect/set_variable): Initialize the variable to 0 using set_variable before calling randomize_variable to avoid script errors from reading undefined variables.
  • [check_variable](/wiki/trigger/check_variable): After randomization completes, use check_variable to evaluate the result range and branch into different game logic accordingly.
  • [clamp_variable](/wiki/effect/clamp_variable): After the random value is generated, use clamp_variable to forcefully constrain it within a safe range, preventing boundary overflow from affecting subsequent calculations.
  • [randomize_temp_variable](/wiki/effect/randomize_temp_variable): If the random result is only used once within the current effect block, use randomize_temp_variable instead for better performance. The syntax is identical; choose based on your needs.

Common Pitfalls

  1. Randomizing a variable without prior declaration: Calling randomize_variable directly on a variable that was never set with set_variable may produce script warnings or silent failures in some versions. Always initialize with set_variable first.
  2. Accidentally passing a temporary variable name: randomize_variable operates on persistent variables. If a variable is named with the temp_ convention but is actually a persistent variable, there will be no logical error, but it can easily be confused with randomize_temp_variable, causing reads across effect blocks to retrieve an uninitialized temporary variable instead of the expected persistent value.