diff --git a/OpenRA.FileFormats/Thirdparty/Random.cs b/OpenRA.FileFormats/Thirdparty/Random.cs index a1c824253c..1eccd6db25 100644 --- a/OpenRA.FileFormats/Thirdparty/Random.cs +++ b/OpenRA.FileFormats/Thirdparty/Random.cs @@ -6,16 +6,16 @@ namespace OpenRA.Thirdparty public class Random { - int[] mt = new int[624]; + uint[] mt = new uint[624]; int index = 0; public Random() : this(Environment.TickCount) { } public Random(int seed) { - mt[0] = seed; - for (var i = 1; i < mt.Length; i++) - mt[i] = 1812433253 * (mt[i - 1] ^ (mt[i - 1] >> 30)) + i; + mt[0] = (uint)seed; + for (var i = 1u; i < mt.Length; i++) + mt[i] = 1812433253u * (mt[i - 1] ^ (mt[i - 1] >> 30)) + i; } public int Next() @@ -24,26 +24,29 @@ namespace OpenRA.Thirdparty var y = mt[index]; y ^= (y >> 11); - y ^= (int)((y << 7) & 2636928640); - y ^= (int)((y << 15) & 4022730752); + y ^= ((y << 7) & 2636928640); + y ^= ((y << 15) & 4022730752); y ^= y >> 18; index = (index + 1) % 624; - return y; + return (int)(y % int.MaxValue); } public int Next(int low, int high) { return low + Next() % (high - low); } public int Next(int high) { return Next() % high; } - public double NextDouble() { return (uint)Next() / (double)uint.MaxValue; } + public double NextDouble() { return Math.Abs(Next() / (double)0x7fffffff); } void Generate() { - for (var i = 0; i < mt.Length; i++) + unchecked { - var y = (mt[i] & int.MinValue) | (mt[(i + 1) % 624] & int.MaxValue); - mt[i] = mt[(i + 397) % 624] ^ (y >> 1); - if ((y & 1) == 1) - mt[i] = (int)(mt[i] ^ 2567483615); + for (var i = 0u; i < mt.Length; i++) + { + var y = (mt[i] & 0x80000000) | (mt[(i + 1) % 624] & 0x7fffffff); + mt[i] = mt[(i + 397u) % 624u] ^ (y >> 1); + if ((y & 1) == 1) + mt[i] = (mt[i] ^ 2567483615); + } } } } diff --git a/OpenRA.Game/WorldUtils.cs b/OpenRA.Game/WorldUtils.cs index 75c7c465ff..37ee35304f 100755 --- a/OpenRA.Game/WorldUtils.cs +++ b/OpenRA.Game/WorldUtils.cs @@ -231,8 +231,8 @@ namespace OpenRA public static float Gauss1D(this Thirdparty.Random r, int samples) { - var xs = Graphics.Util.MakeArray(samples, _ => (float)r.NextDouble() * 2 - 1); - return xs.Sum() / samples; + return Graphics.Util.MakeArray(samples, _ => (float)r.NextDouble() * 2 - 1f) + .Sum() / samples; } // Returns a random offset in the range [-1..1,-1..1] with a separable diff --git a/OpenRA.Mods.Aftermath/DemoTruck.cs b/OpenRA.Mods.Aftermath/DemoTruck.cs index 42a8220f01..616ba7d952 100644 --- a/OpenRA.Mods.Aftermath/DemoTruck.cs +++ b/OpenRA.Mods.Aftermath/DemoTruck.cs @@ -18,9 +18,8 @@ */ #endregion -using OpenRA.Effects; -using OpenRA.Traits; using OpenRA.Mods.RA; +using OpenRA.Traits; namespace OpenRA.Mods.Aftermath { diff --git a/OpenRA.Mods.RA/Mine.cs b/OpenRA.Mods.RA/Mine.cs index 9db8c33d05..5902730945 100644 --- a/OpenRA.Mods.RA/Mine.cs +++ b/OpenRA.Mods.RA/Mine.cs @@ -20,10 +20,9 @@ using System.Collections.Generic; using System.Linq; -using OpenRA.Effects; +using OpenRA.GameRules; using OpenRA.Traits; using OpenRA.Traits.Activities; -using OpenRA.GameRules; namespace OpenRA.Mods.RA {