Pheenoh

TP RNG

Apr 16th, 2020
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.66 KB | None | 0 0
  1. RNG in TP is a floating point value (double) that is returned by using three 32-bit integers to function as the seed. The game seeds these three values at boot with 100, 100, 100 and it is then re-seeded on every subsequent call to the RNG function.
  2.  
  3. Here's the function code:
  4.  
  5. double cM_rnd() {
  6. double RNG_val;
  7.  
  8. // these variable definitions are defined outside the function
  9. RNG0 = (RNG0 * 171) % 30269;
  10. RNG1 = (RNG1 * 172) % 30307;
  11. RNG2 = (RNG2 * 170) % 30323;
  12.  
  13. RNG_val = fmod((double)((float)((double)CONCAT44(0x43300000,RNG2 ^ 0x80000000) - 0x4330000080000000) / 30323.0f +
  14. (float)((double)CONCAT44(0x43300000,RNG0 ^ 0x80000000) - 0x4330000080000000) / 30269.0f +
  15. (float)((double)CONCAT44(0x43300000,RNG1 ^ 0x80000000) - 0x4330000080000000) / 30307.0f),1.0f);
  16. return (double)ABS((float)RNG_val);
  17. }
  18.  
  19. (The CONCAT44 is pseudocode generated by Ghidra that basically means concatenate the 2 float values together.)
  20. (The ABS takes the floating point absolute value before returning the value.)
  21.  
  22. So with the initial seed 100,100,100 the new seed after running through one time will be 17100, 17200, 17000 and the resulting RNG value will be 0.6930906772613525 (hex: 3fe62dcc80000000).
  23.  
  24. Interestingly, the algorithm here for calculating the final RNG value will always return a normalized value between 0.0 and 1.0. This value is then typically multiplied against another value which is changed on a case by case basis depending on the calling function.
  25.  
  26. Here is a small RNG simulator program if you'd like to mess around with the seeding to see what values are returned: https://github.com/Pheenoh/tp-rng-simulator
Add Comment
Please, Sign In to add comment