Wiki

effect · set_variable_to_random

Definition

  • Supported scope:any
  • Supported target:none

Description

sets a variable to a random value. example 
set_variable_to_random = num_dogs #sets num_dogs a random value between [0, 1) 
set_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 
}

实战 · 配合 · 坑

实战内容由 AI 生成,并已对照 vanilla 命令词表校验 —— 请当作起点而非权威依据。上方的定义节才是游戏自带文档原文。

实战用法

set_variable_to_random 常用于 mod 中需要引入随机性的场景,例如随机化某个国家的初始资源储量、随机决定某事件的数值奖励,或是为 AI 行为添加不可预测的权重。下面示例演示在一个事件中随机给玩家分配 5~10 之间的整数补给加成值:

country_event = {
    id = my_mod.1
    immediate = {
        set_variable_to_random = {
            var = random_supply_bonus
            min = 5
            max = 10
            integer = yes
        }
        add_to_variable = {
            var = supply_stockpile
            value = random_supply_bonus
        }
    }
}

配合关系

  • [add_to_variable](/wiki/effect/add_to_variable) —— 生成随机值后,通常需要将其累加到另一个已有变量上,而非直接覆盖。
  • [clamp_variable](/wiki/effect/clamp_variable) —— 随机值写入后可能超出业务逻辑的合理区间,用此命令将结果钳制在安全范围内。
  • [check_variable](/wiki/trigger/check_variable) —— 在 trigger 块中读取刚设置的随机变量,根据其落点决定后续分支逻辑。
  • [if](/wiki/effect/if) —— 配合 check_variable 对随机结果做条件判断,实现"概率分支"效果。

常见坑

  1. max 值是开区间:范围是 [min, max),也就是说结果永远取不到 max 本身。若希望整数结果包含 10,应将 max 设为 11 而非 10,否则 integer = yes 时最大只会生成 9。
  2. 忘记 integer = yes 导致浮点数污染:默认返回浮点数,若后续脚本用该变量做除法或显示给玩家看,小数部分可能引发意外的数值错误或本地化显示问题,需要明确声明 integer = yes 或事后用 [round_variable](/wiki/effect/round_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

set_variable_to_random is commonly used in mods to introduce randomness into scenarios such as randomizing a country's initial resource stockpiles, randomly determining numerical rewards for events, or adding unpredictable weights to AI behavior. The example below demonstrates how to randomly assign a supply bonus between 5 and 10 to the player in an event:

country_event = {
    id = my_mod.1
    immediate = {
        set_variable_to_random = {
            var = random_supply_bonus
            min = 5
            max = 10
            integer = yes
        }
        add_to_variable = {
            var = supply_stockpile
            value = random_supply_bonus
        }
    }
}

Synergy

  • [add_to_variable](/wiki/effect/add_to_variable) — After generating a random value, it typically needs to be accumulated into an existing variable rather than directly overwriting it.
  • [clamp_variable](/wiki/effect/clamp_variable) — Random values may fall outside the reasonable bounds of your game logic after being written; use this command to constrain the result within a safe range.
  • [check_variable](/wiki/trigger/check_variable) — Read the newly set random variable within a trigger block and decide subsequent branching logic based on its value.
  • [if](/wiki/effect/if) — Combined with check_variable, perform conditional checks on random results to achieve a "probabilistic branching" effect.

Common Pitfalls

  1. max is an open interval: The range is [min, max), meaning the result will never reach max itself. If you want an integer result to include 10, set max to 11 rather than 10; otherwise, with integer = yes the maximum generated will only be 9.
  2. Forgetting integer = yes causes floating-point contamination: By default, a floating-point number is returned. If subsequent scripts use this variable for division or display it to players, the decimal portion may cause unexpected numerical errors or localization display issues. Be explicit by declaring integer = yes or use [round_variable](/wiki/effect/round_variable) afterward to round the value.