Hands-On Usage
set_temp_variable_to_random is commonly used in event or decision scenarios that require introducing randomness, such as randomizing a country's resource output, randomly determining event damage values, or generating different random weights for each iteration in a loop. The following example demonstrates randomly generating an integer casualty value in an event and deducting manpower:
immediate = {
set_temp_variable_to_random = {
var = random_casualties
min = 10
max = 51
integer = yes
}
add_manpower = {
value = random_casualties
multiply = -1
}
custom_effect_tooltip = random_casualties_tt
}
Synergy
[clamp_temp_variable](/wiki/effect/clamp_temp_variable): After the random value is generated, use this command to forcibly constrain the result within a safe range, preventing extreme values from breaking script logic.
[multiply_temp_variable](/wiki/effect/multiply_temp_variable): Scale the random result by multiplying it with a coefficient, for example converting a raw value in the [0, 1) interval to a magnitude that meets actual requirements.
[add_to_temp_variable](/wiki/effect/add_to_temp_variable): Stack a fixed offset on top of the random value, implementing the typical design pattern of "base value + random fluctuation".
[check_variable](/wiki/trigger/check_variable): In subsequent trigger blocks, check the generated temporary variable and decide whether to trigger branch conditions based on the random result.
Common Pitfalls
- Confusing shorthand syntax with block syntax
min/max defaults: When using shorthand form (directly assigning the variable name), the range is fixed at [0, 1). If used directly in an integer context without adding integer = yes and custom ranges, you will almost always get 0. Use the complete block syntax instead and explicitly declare min, max, and integer.
max value is an open interval upper bound and cannot be reached: Beginners often mistakenly believe that max = 10 will produce results including 10, but the actual range is [min, max). If you need integer results to include 10, set max to 11.