# View Page Source

Revision (current)
Last Updated by Samsara on 8/7/2023 3:21 AM

```[https://files.tasvideos.org/gameresources/nes/dw2/dw2.png|right]

Luck manipulation is the act of controlling how the game determines "random" results through input. It has many names, including "luck abuse", "randomness abuse", or "RNG abuse".

We should emphasize that the computer, being a deterministic machine, has no concept of randomness. All it does is follow instructions. To simulate a stream of effectively random numbers, the computer uses a ''[http://en.wikipedia.org/wiki/Pseudorandom_number_generator|pseudo-random number generator]'' (often called ''random number generator'' and known as a ''RNG'' or ''PRNG''). The game uses these RNG values to calculate random decisions, such as:

* Attack misses and critical hits (every turn-based RPG ever)
* Dice rolls (board games, casino games)
* Boss behavior (Mega Man games)
* Particle effects

This is where luck-manipulation comes in. Luck manipulation is the act of controlling random decisions through input so that the results occur in your favor.

!!! What you need to know about the RNG in the game

!! When does the game cycle the RNG?

In order to be of any use, the game must cycle the RNG at least once when determining random events. However, there are differing approaches to when the game cycles the RNG.

* Some games simply cycle the RNG once per frame. A simple approach.
* Some games cycle the RNG frequently, using random numbers often for minor, usually cosmetic purposes.
* Some games cycle the RNG rarely, only using it for important events.

!! What seed value does the RNG use?

All RNGs must have a starting value, called the seed. This is what prevents the RNG from giving the same number sequence each time. It can come from a variety of sources and it is common that additional data is added on the fly to enhance randomness.

* Some games use a hardcoded seed. If the seed is reset between levels then this allows easy cut and paste of input.
* Some games persist the seed value in the save file.
* Some games run a counter during moments of the game, such as the initial menus, and use that as the starting value.
* The current time is an extremely popular seed value on systems that have realtime clocks.
* DS games like to use the firmware player profile as a seed value.
* Some games directly seed using the player controller input.
* Some games use very volatile low level realtime measurements like the exact time it took the game program to process a frame.

!! Which RNG does the game use for each action?
Games sometimes run multiple RNGs, using them for different purposes. It is important to know which RNG is used for which actions in the game.

!! When is the random value selected?

Let's say you are TASing Pokemon and your Pokemon, normally slower, is holding Quick Claw, so it has a 1/4 chance of going first. You try to manipulate by delaying before selecting the attack. However, no matter how many frames you delay, your Pokemon will never go first. What went wrong?

It is very important to find out when the game has decided the random action. Otherwise, you might be wasting rerecords trying to manipulate something that has been determined not to work.

In the case above, Quick Claw activation was decided before the menu appeared for your current turn. Since Quick Claw was already determined not to work, your Pokemon will always go second.

If a scenario appears where you can't manipulate an action that should be able to be manipulated, try searching backward (in the movie) until you discover when inputs can change the result.

!! The RNG formula

The pseudo-random number generator is just an algorithm that produces a sequence of numbers that is not easily predictable, for the purpose of the game. In general, determining the algorithm of the RNG is not easy, and finding out how the game uses these random numbers is even more difficult.

There are well-known algorithms for producing numbers such as:

* [http://en.wikipedia.org/wiki/Linear_Congruential_Generators|Linear congruential generators].
* [http://en.wikipedia.org/wiki/Linear_feedback_shift_register|Linear feedback shift registers].

There are [https://en.wikipedia.org/wiki/List_of_random_number_generators|some popular RNG formulas], but games on older systems tend to implement their own. On the flip side, modern games tend to use the runtime library provided one.

Common RNGs share a property with standard cryptographic code: certain key numbers tend to be used in many different implementations. A simple search in the code for the key number can reveal a common RNG implementation.

Less secure RNGs are often reversible, there is a formula to produce numbers in the exact opposite order. This is of limited use due to savestates, but the possibility exists none the less.

! Linear congruential generators

The name of these kinds of functions is just a description in technical jargon of the steps involved. But it's very simple, so here's a full explanation:
* First, take the previous output {{X[[n]}} (or the seed if there is none), multiply by a constant scaling factor {{a}}, and add a constant {{c}}. (An "affine" or "linear" transformation.)
* Then, divide by the modulus {{m}}. The remainder is the new output. (The remainder is said to be "congruent, mod m".)
That's it. {{X[[n + 1] := (a*X[[n] + c) % m}}.

The forum post [Forum/Topics/19515|"Finding and predicting RNG in practice (how to)"] gives a detailed example of finding the address of a game's RNG, finding and disassembling the RNG function, and identifying the constants {{a}}, {{c}}, and {{m}}.

There's a mathematical quirk about LCGs with modulus parameters which are powers of 2 (which is common because it has near-zero computation cost, it's just a truncation) that can help you quickly identify when one is used: the generated numbers alternate odd and even.
However, if the game devs noticed this, or they copied an implementation meant for cryptographic use, then they'd only use the most-significant bits outside the generator.

! Tables and precomputation
Some random number generators do not use a formula at all. Instead they have a table of prepared random numbers that they read from in order. The seed in this case is where in the table the generator begins reading.

Higher end random number generators may use amortized algorithms. This means that they do some operations to generate data for future numbers in bulk and then hand out the pregenerated data until it runs out. For example, the RNG may be reseeded once every million times it is invoked using expensive to collect data.

!! Fake RNG
Games sometimes exhibit behavior that seems random, but is not tied to the RNG. Instead the behavior is tied to something that is close enough to randomness, such as the exact tick of the global game timer or the exact position of an entity.

!!! Luck manipulation

!! How to manipulate the seed

Chances are, you can affect random decisions through input.

[http://www.fceux.com/web/help/taseditor/lib/dw-luck_manipulation.gif|right]

* If the game seeds using the controller, then there are many, many options here, and you can manipulate almost without loss of time, even though it is harder to optimize, because there are so many inputs that affect the RNG.

* If the game cycles the RNG once per frame, delaying button presses will be necessary, but is also sufficient for manipulation. Here, timing is the key.

* If the game cycles during an easily accessible action, such as moving or shooting, then performing that action will easily manipulate the RNG cycling.

* If the game only ever cycles the RNG through actions that are not easily accessible or through the action you want to manipulate, then it is very hard to get what you want. Some sacrifice may be required. RNG understanding and scenario construction are extremely useful here.

* If the game seeds using volatile measurements then there is usually no other option than brute forcing things by trying all possible paths. Changing resource utilization, like the active enemies, tends to cause larger changes.

The GIF of Darkwing Duck on the right shows how delaying a frame each time causes a different drop.

!! AI

In games where decision-making is involved, the game will often have a decision-making process for the computer players, colloquially called "AI". You are limited by what the AI can do.

Say the AI can make a good move, and you want it to make some other move. If making that good move is the only result of the AI's decision, there is nothing you can do about it. The only thing you can do is to try to arrange a scenario where the AI is not in a position to make that move or regard it as a good move.

This also applies to flawed AI decisions that regard bad moves (to human players) as good moves. The only factor is the AI's insistence on making that move.

! Other issues

Especially in games where enemy behavior is to be manipulated, check to make sure that the circumstance allows manipulation.

* Some enemies have set behaviors.
* Some always use a particular attack under some circumstance. It could be when at low health, or depending on your position or status.

Remember that you can only bend a game as far as its game mechanics will allow. Some games use a damage mechanic, with some form of randomness. You can't do more damage than the maximum possible value calculated by the mechanic, even if randomness goes your way.

!! Rarity

While not technically a problem, rarity (usually on the order of 1/1000 or worse) of an event, or a sequence of events, can be perceived as one due to the non-occurrence of the event in any reasonable amount of time. There are ways to deal with it:

* See below for tips on how to improve luck-manipulation. Specifically, subdividing a manipulation of a sequence of events into multiple manipulations may increase the probability of the whole sequence.
* Understand how the formula works, then apply it for this event.
* Use Lua or program a simulator to brute-force for this event.
* Give up.

!!! Tips for luck manipulation

!! Subdivision of a sequence of events

Suppose that you want to manipulate three events, each of which occur shortly after the other. Each event has 1/16 chance of occurring, so the total probability is (1/16)^3, or 1/4096, which isn't very high.

However, if it is possible to insert meaningful input (such as delays) between these events, then instead of one manipulation for three events, it becomes three manipulations, each for one event. This is much easier to manipulate because each event has probability 1/16.

It is much easier to manipulate many outcomes, if there is a high degree for which controllable input can manipulate these outcomes one block at a time.

!! RNG memory address, and monitoring

Wouldn't it help to know which actions cycle the RNG?

This is where monitoring the RNG in memory is useful. By observing when it changes and how, one can figure out if some action can be used to help manipulation.

For example, suppose the RNG in memory tends not to change, but changes whenever you shoot a particular weapon. Then you know that shooting this weapon is useful for luck manipulation. Furthermore, you witness some enemy behaviors changing the RNG as well. You know then that this behavior can be manipulated.

Even if the RNG always changes, sometimes some action causes it to change more than usual. This usually means that this action is useful for manipulation.

You need to find the RNG in memory first. See [Memory Search] for details. Then watch it in memory.

!!! Examples

One of the prime examples of luck manipulation is the [1482M|Dragon Warrior] movie. The player abuses luck in his movie of this game in different ways.
* All undesired enemy encounters are avoided by stopping his motion for a frame or two at various times along the player's path.
* When encountering enemy, its HP is manipulated to be as low as possible.
* Critical hits are gotten 100% of the time by scrolling through the battle menu and waiting to attack on a particular frame. A single frame of difference would not have resulted in a critical hit.
* When using an enemy encounter to suicide, the enemy is manipulated to attack first.