From 3023254d870235e6d75bf45d143abbde93485366 Mon Sep 17 00:00:00 2001 From: Kamron Batman <3953314+kamronbatman@users.noreply.github.com> Date: Sat, 15 May 2021 20:15:22 -0700 Subject: [PATCH] fix(core): Changes random source to use debut assert (#603) Changes RNG to not crash in release mode with bad values. --- Projects/Server/Random/BaseRandomSource.cs | 22 +++++++--------------- 1 file changed, 7 insertions(+), 15 deletions(-) diff --git a/Projects/Server/Random/BaseRandomSource.cs b/Projects/Server/Random/BaseRandomSource.cs index 1c1dbaa78..a50197598 100644 --- a/Projects/Server/Random/BaseRandomSource.cs +++ b/Projects/Server/Random/BaseRandomSource.cs @@ -14,6 +14,7 @@ *************************************************************************/ using System; +using System.Diagnostics; using System.Numerics; using System.Runtime.CompilerServices; @@ -42,12 +43,9 @@ namespace Server.Random [MethodImpl(MethodImplOptions.AggressiveInlining)] public int Next(int count) { - if (count == 0) - { - throw new ArgumentOutOfRangeException(nameof(count), count, "count must not be 0"); - } + Debug.Assert(count != 0, $"{nameof(count)} must not be 0"); - if (count == 1 || count == -1) + if (count is -1 or 0 or 1) { return 0; } @@ -73,12 +71,9 @@ namespace Server.Random [MethodImpl(MethodImplOptions.AggressiveInlining)] public uint Next(uint count) { - if (count == 0) - { - throw new ArgumentOutOfRangeException(nameof(count), count, "count must not be 0"); - } + Debug.Assert(count != 0, $"{nameof(count)} must not be 0"); - if (count == 1) + if (count is 0 or 1) { return 0; } @@ -100,12 +95,9 @@ namespace Server.Random [MethodImpl(MethodImplOptions.AggressiveInlining)] public long Next(long count) { - if (count == 0) - { - throw new ArgumentOutOfRangeException(nameof(count), count, "count must not be 0"); - } + Debug.Assert(count != 0, $"{nameof(count)} must not be 0"); - if (count == 1 || count == -1) + if (count is -1 or 0 or 1) { return 0; }