Page MenuHomeFeedback Tracker

Math Functions not properly seeded on instance start
Awaiting internal Testing, NormalPublic

Description

Game Version number: 1.2.0.124
Modded?: (Yes/No) N/A
If modded, please list the mods: N/A

Issue Description:
So after another schizo inducing dilemma, a bit of trouble shooting and feedback on the official Discord... The seed value used by Math method is not working as intended in a default scenario. It appears to be utilizing a static value before the function OnGameStart in BaseGameMode entity.

As an example I started a new game instance in workbench 4 times, all 4 times before I call Math.Randomize(-1) it always returns the same values. After calling Math.Randomize(-1) it functions as expected. This behavior is no different in a dedicated server environment or client.

WORKBENCH START 1
SCRIPT       : RandomInt(0, 680843)): 673030
SCRIPT       : RandomFloat01(): 0.445692
Call Math.Randomize(-1);
SCRIPT       : RandomInt(0, 680843)): 351714
SCRIPT       : RandomFloat01(): 0.348155
WORKBENCH START 2
SCRIPT       : RandomInt(0, 680843)): 673030
SCRIPT       : RandomFloat01(): 0.445692
Call Math.Randomize(-1);
SCRIPT       : RandomInt(0, 680843)): 554926
SCRIPT       : RandomFloat01(): 0.142766
WORKBENCH START 3
SCRIPT       : RandomInt(0, 680843)): 673030
SCRIPT       : RandomFloat01(): 0.445692
Call Math.Randomize(-1);
SCRIPT       : RandomInt(0, 680843)): 345460
SCRIPT       : RandomFloat01(): 0.636433
WORKBENCH START 4
SCRIPT       : RandomInt(0, 680843)): 673030
SCRIPT       : RandomFloat01(): 0.445692
Call Math.Randomize(-1);
SCRIPT       : RandomInt(0, 680843)): 177384
SCRIPT       : RandomFloat01(): 0.727989

After discussing it on Discord NiiRoZz chimed in about not using the Math method and instead to use RandomGenerator because of other mods potentially setting a fixed seed and not resetting it. Ok sure, I can do that no problem. Except the issue is array.GetRandomIndex() also uses the math method so.. yeah...

Solutions

  1. Fix the initial seed for Math method
  2. Change array.GetRandomIndex to use RandomGenerator instead of Math.RandomInt
  3. Leave a note about the use cases of Math.Randomize and how Math.Randomize should be reset to -1 after desired use is done

Details

Severity
Tweak
Resolution
Open
Reproducibility
Always
Operating System
Windows 10 x64
Category
General
Steps To Reproduce

Use the following code early on in something like a GameSystem module:

Print("RandomInt(0, 680843)): " + Math.RandomInt(0, 680843));
Print("RandomFloat01(): " + Math.RandomFloat01());
Math.Randomize(-1);
Print("RandomInt(0, 680843)): " + Math.RandomInt(0, 680843));
Print("RandomFloat01(): " + Math.RandomFloat01());
Additional Information

There seems to be no point to having a fixed seed or the ability to set it in the first place. The point of random is to be random. Regardless, at least fix the array.GetRandomIndex otherwise that function is completely pointless if modders can cause undesired affects.

Event Timeline

Tonic-_- edited Additional Information. (Show Details)Aug 24 2024, 12:30 PM
Geez changed the task status from New to Awaiting internal Testing.Aug 26 2024, 3:43 PM