RNG: Difference between revisions

From Ukikipedia
Jump to navigation Jump to search
(lots of data and math)
Line 1: Line 1:
{{stub}}
{{stub}}
'''RNG''' or '''Random Number Generation''' is the game's method of generating a random short. By doing this, the game can make things like dust movement seem random.
'''RNG''' or '''Random Number Generation''' is the game's method of generating a random short. By doing this, the game can make things like dust movement seem random<ref>https://www.youtube.com/watch?v=MiuLeTE2MeQ</ref>.
==Technical description==
==Technical description==
The RNG variable is a short, which is a number from 0 to 65535 inclusive. When an [[object]], [[snow]], or something else wants to call RNG, it takes the current RNG value and runs it through the '''RNG function''', below. It uses the result in its own calculations and also stores the result to the RNG variable. Thus, the RNG variable is equal to the last used RNG value.
The RNG variable is a short, which is a number from 0 to 65535 inclusive. When an [[object]], [[snow]], or something else wants to call RNG, it takes the current RNG value and runs it through the '''RNG function''', below. It uses the result in its own calculations and also stores the result to the RNG variable. Thus, the RNG variable is equal to the last used RNG value. Every [[frame]], every object is iterated through in order of [[object slot]]s and may choose whether to call RNG once, multiple times, or not at all.
===The RNG function===
===The RNG function===
  <nowiki>
  <nowiki>
Line 22: Line 22:
===Description===
===Description===
[[File:RNG Graph.png|500px|right|A graph showing notable RNG values and indeces in SM64.]]
[[File:RNG Graph.png|500px|right|A graph showing notable RNG values and indeces in SM64.]]
The function has 65536 possible inputs and 65536 possible outputs. It is a bijection, meaning that every input maps to exactly one output and none repeat or are left out. The function forms two loops, one of length 65534 and one of length 2, but one of the if statements causes the cycle of 2 to lead back to the cycle of 65534. Oddly, the RNG value of 21674 at index 65113 loops back to index 0. This may be because this index's S1 value is equal to the previous RNG value for the first time, but that is no reason to prematurely end the loop. Indeces 65114-65533, as well as values 22026 and 58704 (the loop of 2,  which are not given indeces) are considered '''impossible RNG values''' and cannot be reached without hacking. The RNG index and value of 0 is set when the game powers on.
The function has 65536 possible inputs and 65536 possible outputs. It is a bijection, meaning that every input maps to exactly one output and none repeat or are left out. The function forms two loops, one of length 65534 and one of length 2, but one of the if statements causes the cycle of 2 to lead back to the cycle of 65534. Oddly, the RNG value of 21674 at index 65113 loops back to index 0. This may be because this index's S1 value is equal to the previous RNG value for the first time, but that is no reason to prematurely end the loop. Indeces 65114-65533, as well as values 22026 and 58704 (the loop of 2,  which are not given indeces) are considered [https://www.youtube.com/redirect?v=MiuLeTE2MeQ&redir_token=SymUNd8wBHOCXi28TSwbDD5ZZ9N8MTUzOTI1NzkyM0AxNTM5MTcxNTIz&event=video_description&q=https%3A%2F%2Fdrive.google.com%2Ffile%2Fd%2F0B3JCRPC09lDtcHRjdU9pSjJCX1U%2Fview%3Fusp%3Dsharing|'''impossible RNG values'''] and cannot be reached without hacking. All other RNG values are considered [https://www.youtube.com/redirect?v=MiuLeTE2MeQ&redir_token=SymUNd8wBHOCXi28TSwbDD5ZZ9N8MTUzOTI1NzkyM0AxNTM5MTcxNTIz&event=video_description&q=https%3A%2F%2Fdrive.google.com%2Ffile%2Fd%2F0B3JCRPC09lDtOExmOW1hclhiNHc%2Fview%3Fusp%3Dsharing|'''possible RNG values''']. The RNG index and value of 0 is set when the game powers on.
 
==Cycling RNG==
Because RNG indeces loop from 0 to 65113 and back to 0 again, by looping through every RNG index Mario can reach any reachable RNG value. By making [[dust]], Mario can advance RNG 4 times per frame, which takes 9 minutes to loop. If objects are calling RNG, this time will be much faster.
==Objects that call RNG==
==Objects that call RNG==
* [[Dust]] calls RNG to determine its resultant [[angle]] and [[horizontal speed]]. Note that the dust itself calls RNG, not the [[dust spawner]].
* Dust calls RNG to determine its resultant [[angle]] and [[horizontal speed]]. Note that the dust itself calls RNG, not the [[dust spawner]].
* [[Goomba]]s call RNG to determine which direction to move when they do not see Mario.
* [[Goomba]]s call RNG to determine which direction to move when they do not see Mario.
* [[Bob-omb]]s call RNG almost every frame to determine whether to [[blink]].
* [[Bob-omb]]s call RNG almost every frame to determine whether to [[blink]]. If the value that they receive is less than 656, they blink. Otherwise, they don't. Taking into account impossible RNG, the chance of a Bob-omb blinking on a given frame is 0.0099785.
* Although not an object, [[snow]] also calls RNG.
* Although not an object, [[snow]] also calls RNG.
* [[Coin]]s from Bob-ombs, Goombas, [[Big Cork Box]]es, [[Cork Box]]es, [[Piranha Plant]]s, [[Bully|Bullies]], [[Chuckya]]s, [[Fly Guy]]s, [[Snowman|Snowmen]], etc... you name it, it calls RNG to determine angle, horizontal speed, and [[vertical speed]].
* [[Coin]]s from Bob-ombs, Goombas, [[Big Cork Box]]es, [[Cork Box]]es, [[Piranha Plant]]s, [[Bully|Bullies]], [[Chuckya]]s, [[Fly Guy]]s, [[Snowman|Snowmen]], etc... you name it, it calls RNG 3 times to determine angle, horizontal speed, and [[vertical speed]]. Since angles are shorts just like RNG, the RNG value is used directly for the angle. Horizontal speed is set to (RNG / 65536) * 20, and vertical speed is set to (RNG / 65536) * 40 + 17. Since these RNG calls must be contiguous, there are still only 65114 possible coin trajectories.
* [[Bowser]] calls RNG to determine which attack to do next.
* [[Bowser]] calls RNG to determine which attack to do next.
* [[Spinner]]s call RNG twice when they stop spinning on the random setting. The first call determines direction. If the result is less than 32768, the spinner spins clockwise, and otherwise counterclockwise. The chance of a spinner choosing to spin clockwise on a given frame is 0.50016893. Spinners calculate their intended angular displacement by the formula (RNG % 4) * 30 + 30. Thus, the chance of spinning 30 [[angle unit]]s is 0.24982339, the chance of spinning 60 angle units is 0.24999232, the chance of spinning 90 angle units is 0.25008447, and the chance of spinning 120 angle units is 0.25009982.
* And many, many, many, many, many more.
* And many, many, many, many, many more.
==References==
<references />

Revision as of 02:36, 11 October 2018

RNG or Random Number Generation is the game's method of generating a random short. By doing this, the game can make things like dust movement seem random[1].

Technical description

The RNG variable is a short, which is a number from 0 to 65535 inclusive. When an object, snow, or something else wants to call RNG, it takes the current RNG value and runs it through the RNG function, below. It uses the result in its own calculations and also stores the result to the RNG variable. Thus, the RNG variable is equal to the last used RNG value. Every frame, every object is iterated through in order of object slots and may choose whether to call RNG once, multiple times, or not at all.

The RNG function

unsigned short rng_function (unsigned short input) {
    if (input == 0x560A) input = 0;
    unsigned short S0 = (unsigned char)input << 8;
    S0 = S0 ^ input;
    input = ((S0 & 0xFF) << 8) | ((S0 & 0xFF00) >> 8);
    S0 = ((unsigned char)S0 << 1) ^ input;
    short S1 = (S0 >> 1) ^ 0xFF80;
    if ((S0 & 1) == 0) {
        if (S1 == 0xAA55) input = 0;
        else input = S1 ^ 0x1FF4;
    }
    else input = S1 ^ 0x8180;
    return (unsigned short)input;
}

Description

A graph showing notable RNG values and indeces in SM64.

The function has 65536 possible inputs and 65536 possible outputs. It is a bijection, meaning that every input maps to exactly one output and none repeat or are left out. The function forms two loops, one of length 65534 and one of length 2, but one of the if statements causes the cycle of 2 to lead back to the cycle of 65534. Oddly, the RNG value of 21674 at index 65113 loops back to index 0. This may be because this index's S1 value is equal to the previous RNG value for the first time, but that is no reason to prematurely end the loop. Indeces 65114-65533, as well as values 22026 and 58704 (the loop of 2, which are not given indeces) are considered impossible RNG values and cannot be reached without hacking. All other RNG values are considered possible RNG values. The RNG index and value of 0 is set when the game powers on.

Cycling RNG

Because RNG indeces loop from 0 to 65113 and back to 0 again, by looping through every RNG index Mario can reach any reachable RNG value. By making dust, Mario can advance RNG 4 times per frame, which takes 9 minutes to loop. If objects are calling RNG, this time will be much faster.

Objects that call RNG

  • Dust calls RNG to determine its resultant angle and horizontal speed. Note that the dust itself calls RNG, not the dust spawner.
  • Goombas call RNG to determine which direction to move when they do not see Mario.
  • Bob-ombs call RNG almost every frame to determine whether to blink. If the value that they receive is less than 656, they blink. Otherwise, they don't. Taking into account impossible RNG, the chance of a Bob-omb blinking on a given frame is 0.0099785.
  • Although not an object, snow also calls RNG.
  • Coins from Bob-ombs, Goombas, Big Cork Boxes, Cork Boxes, Piranha Plants, Bullies, Chuckyas, Fly Guys, Snowmen, etc... you name it, it calls RNG 3 times to determine angle, horizontal speed, and vertical speed. Since angles are shorts just like RNG, the RNG value is used directly for the angle. Horizontal speed is set to (RNG / 65536) * 20, and vertical speed is set to (RNG / 65536) * 40 + 17. Since these RNG calls must be contiguous, there are still only 65114 possible coin trajectories.
  • Bowser calls RNG to determine which attack to do next.
  • Spinners call RNG twice when they stop spinning on the random setting. The first call determines direction. If the result is less than 32768, the spinner spins clockwise, and otherwise counterclockwise. The chance of a spinner choosing to spin clockwise on a given frame is 0.50016893. Spinners calculate their intended angular displacement by the formula (RNG % 4) * 30 + 30. Thus, the chance of spinning 30 angle units is 0.24982339, the chance of spinning 60 angle units is 0.24999232, the chance of spinning 90 angle units is 0.25008447, and the chance of spinning 120 angle units is 0.25009982.
  • And many, many, many, many, many more.

References