πŸ’» Random Number Generators

RNG in videogames

In computer games, randomness (such as landing a critical hit, the dropping of loot or the behavior of opponents) is typically implemented not through truly random physical processes, but via pseudo-random number generators (RNGs). These are deterministic algorithms that produce a sequence of numbers which, while appearing random, are entirely determined by an initial value known as a "seed".

X_{n+1} = (a * X_n + c) mod m

Where do games get the seed? Most often, they use the system time (for example, the number of milliseconds since the program started). This ensures that the sequence will be different every time the game is launched.

Seed also can be mixed with:

  • process ID
  • mouse coordinates
  • keystrokes
  • RNG examples in games

    • Critical damage in RPGs (WoW, Diablo): crit chance is 20%. A number between 0 and 1 is generated; if it is < 0.2, damage is multiplied by 2.
    • Lootboxes and drops: an item has 1% chance of dropping. On average, it would take 100 monster kills for it to drop. Some games use PRD (pseudo-random distribution) to smooth out streaks of bad luck.
    • Minecraft: a world based on a seed. Speedrunners use known seeds to find resources or quicker routes towards the objective.

    How do developers combat predictability?

  • Change the seed more often β€” for example, every turn, every player's action, every frame.
  • Mix several sources of entropy β€” time, frame counter, click coordinates.
  • Smooth out randomness β€” in some games, a critical hit is guaranteed to occur after a certain number of misses.
  • Why not use true randomness?

    True randomness (for example, by measuring radioactive decay or thermal noise) requires specialized hardware or slow system calls. For a game, this is both too costly and too unpredictable. Furthermore, players tend to dislike true randomness. A streak of three consecutive misses, which might have a 1% probability of occurring, will inevitably happen from time to time, yet players will perceive this as a "broken generator". For this reason, many games employ a "fair" pseudorandom number generator that includes subtle adjustments.

    Expected value in RNG mechanics

    The expected value (EV) is the average outcome that can be anticipated from a random process over a large number of repetitions. For any RNG-based event with a probability \(p\) and a reward \(V\) (such as damage, gold, or experience), the expected gain for a single trial is \(p \cdot V\). If an event has multiple possible outcomes, the EV is calculated as the sum of the products of each outcome's probability and its corresponding value.

    For example, a legendary item dropping: the chance is 2% and the item is worth 5,000 in-game coins. The EV of a single attempt is \(0.02 \times 5,000 = 100\) coins. If killing a monster costs 50 coins (in terms of resources and time), then, on average, you come out ahead. In reality, however, the variance is high. You could make 200 attempts and get nothing, which is why players often demand not just a positive EV, but also some form of guarantee (such as a "pity" counter).

    For a critical hit with a 20% chance and a 2x multiplier, given a base damage of 100: EV = \(0.8 Γ— 100 + 0.2 Γ— 200 = 120\). This is 20% higher than the base damage. This is precisely why characters with a high critical hit chance deal, on average, more damage than those with high base damage β€” all other factors being equal.

    Pseudo-random distribution (PRD) and its impact on EV

    Dota 2, World of Warcraft and many other games use PRD (pseudo-random distribution) to smooth out streaks. Instead of a fixed probability \(p\), a dynamic chance is employed β€” one that increases after each unsuccessful attempt and resets following a success. For instance, with a base 20% chance, the first hit might have a 10% chance to crit, the second 20%, the third 30% and so on. While the average probability remains 20%, the likelihood of experiencing long strings of failures is drastically reduced.

    The expected number of attempts until the first success in PRD remains \(1/p\), just as it is with a standard RNG, but the distribution becomes narrower (with lower variance). Players subjectively perceive this type of randomness as "fairer", because they are less likely to experience the frustration of 10 consecutive failures.

    Formally, for a PRD with a nominal probability \(p\), the probability of success on the \(k\)-th trial is approximately \(p_k = \min(1, c \cdot k)\), where \(c\) is chosen such that the average probability equals \(p\). The expected number of trials, \(\mathbb{E}[N] = \sum_{k=1}^\infty k \cdot P(\text{first success at } k)\), remains \(1/p\), but the second moment (and thus the variance) is lower than that of a geometric distribution.

    Expected number of attempts to obtain a rare item

    Suppose an item drops with a probability \(p\). The number of attempts required to obtain the item for the first time is a random variable that follows a geometric distribution. Its expected value is \(1/p\). For instance, if \(p = 0.01\) (1%), it takes an average of 100 attempts. However, the median (the point by which 50% of players will have obtained the item) is \(\lceil \log_{0.5}(1-p) \rceil \approx 69\) attempts, while the 90th percentile is approximately 230 attempts. This means that some players will end up "farming" for 2–3 times longer than the average.

    Developers often use a "pity timer" (for example, in Genshin Impact): a guaranteed legendary item after 90 roll attempts. This truncates the tail of the distribution and the actual expected number of attempts becomes less than \(1/p\), because the probability of exceeding 90 is zero. The new EV can be calculated as: \[ \mathbb{E}[\text{attempts}] = \sum_{k=1}^{89} k \cdot (1-p)^{k-1}p + 90 \cdot (1-p)^{89}. \] For \(p=0.006\) (0.6%), this results in about 74 attempts, compared to the theoretical 166. This approach reduces variance and increases player satisfaction.

    Law of large numbers and RNG in the long run

    Over a large number of trials (for example, thousands of critical hits or loot drops), the relative frequency of an event tends toward its probability and the average gain approaches its EV. This allows casinos and game developers to rely on EV to balance their economies. For instance, if a player earns an average of 100 gold per battle and an upgrade costs 5,000 gold, the expected number of battles required for that upgrade is 50. However, due to the nature of randomness, an individual player might secure the upgrade in as few as 20 battles or it might take them as many as 120.

    Practical takeaway: when farming for resources, do not rely on short streaks. Focus on the average but be prepared for deviations. Make use of "pity" systems if the game offers them and avoid falling for the gambler’s fallacy.

    Comparing EV for different loot selection strategies

    In some games (e.g., Borderlands, Diablo), a player may choose between several sources of loot, each with a different probability of yielding a desired item. Consider two chests: chest A offers a 5% chance of containing a legendary item, while chest B offers a 2% chance but can be opened twice as fast. The EV per unit of time is as follows: for chest A, it is \(0.05 \cdot V\) over 10 seconds, or \(0.005V\) per second; for chest B, it is \(0.02 \cdot V\) over 5 seconds, or \(0.004V\) per second. Chest A is more advantageous, despite the longer time required to open it.

    It is also important to consider the cost of an attempt (such as health or consumable resources). If each attempt costs 100 gold and a successful outcome yields 5,000, the net EV is \(p \cdot 5,000 - 100\). At \(p = 0.02\), the EV is \(0.02 \cdot 5,000 - 100 = 0\) (a fair game). At \(p = 0.03\), the EV is \(150 - 100 = 50\) gold per attempt on average.

    Conclusion

    Understanding expected value, variance and the characteristics of pseudorandom number generators enables players to make more rational decisions, such as when to open a loot box, which equipment to choose or which seeds to rely on during a speedrun. Developers, for their part, use these same concepts to balance difficulty, create "fair" randomness and manage audience engagement.