Skip to content

Commit

Permalink
Switch from Unity's Random to System Random, fix always getting the s…
Browse files Browse the repository at this point in the history
…ame seeds.
  • Loading branch information
NathanKell committed Nov 15, 2021
1 parent 2c757d9 commit 1182d05
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
6 changes: 4 additions & 2 deletions Source/Engines/ModuleEnginesRF.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,8 @@ public class ModuleEnginesRF : ModuleEnginesSolver

protected bool started = false; // Track start state, don't handle MEC notification before first start.

protected static System.Random staticRandom = new System.Random();

#endregion

#region Overrides
Expand Down Expand Up @@ -421,8 +423,8 @@ public override void Start()
{
if (partSeed == -1)
{
calculatedResiduals = localResidualsThresholdBase + UnityEngine.Random.Range(0f, (float)localVaryResiduals);
partSeed = UnityEngine.Random.Range(0, int.MaxValue);
calculatedResiduals = localResidualsThresholdBase + staticRandom.NextDouble() * localVaryResiduals;
partSeed = staticRandom.Next();
}
}

Expand Down
3 changes: 2 additions & 1 deletion Source/Engines/SolverRF.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public class SolverRF : EngineSolver
private double runVaryIsp = 0d;
private double runVaryMR = 0d;
private System.Random seededRandom;
private static System.Random staticRandom = new System.Random();
private bool pressure = true, ullage = true, disableUnderwater;
private double scale = 1d; // scale for tweakscale

Expand Down Expand Up @@ -338,7 +339,7 @@ protected double GetRandom(bool useSeed)
if (useSeed)
return seededRandom.NextDouble() * 2d - 1d;
else
return UnityEngine.Random.Range(-1f, 1f);
return staticRandom.NextDouble() * 2d - 1d;
}

protected void GetVariances(bool useSeed, out double varianceFlow, out double varianceMR, out double varianceIsp)
Expand Down

0 comments on commit 1182d05

Please sign in to comment.