# HG changeset patch # User Michael Johnson # Date 1606494126 21600 # Fri Nov 27 10:22:06 2020 -0600 # Node ID 8a46b2c476796d450b2663aebd5873afd76b8e89 # Parent 4303888ddcc6f9d6c4cb1723cff081c3d268e684 Fix a couple of bugs in UniformInt diff --git a/src/RandN/Distributions/UniformInt.cs b/src/RandN/Distributions/UniformInt.cs --- a/src/RandN/Distributions/UniformInt.cs +++ b/src/RandN/Distributions/UniformInt.cs @@ -55,7 +55,7 @@ throw new ArgumentOutOfRangeException(nameof(high), $"{nameof(high)} ({high}) must be higher than or equal to {nameof(low)} ({low})."); var unsignedMax = System.UInt32.MaxValue; - var range = unchecked((System.UInt32)(high - low + 1)); + var range = unchecked((System.UInt32)(System.Byte)(high - low + 1)); var intsToReject = range == 0 ? 0 : (unsignedMax - range + 1) % range; return new SByte(low, range, (System.Byte)intsToReject); @@ -84,7 +84,7 @@ var unsigned = rng.NextUInt32(); if (_range == 0) // 0 is a special case where we sample the entire range. { - result = (System.SByte)unsigned; + result = unchecked((System.SByte)unsigned); return true; } @@ -145,7 +145,7 @@ throw new ArgumentOutOfRangeException(nameof(high), $"{nameof(high)} ({high}) must be higher than or equal to {nameof(low)} ({low})."); var unsignedMax = System.UInt32.MaxValue; - var range = unchecked((System.UInt32)(high - low + 1)); + var range = unchecked((System.UInt32)(System.UInt16)(high - low + 1)); var intsToReject = range == 0 ? 0 : (unsignedMax - range + 1) % range; return new Int16(low, range, (System.UInt16)intsToReject); @@ -174,7 +174,7 @@ var unsigned = rng.NextUInt32(); if (_range == 0) // 0 is a special case where we sample the entire range. { - result = (System.Int16)unsigned; + result = unchecked((System.Int16)unsigned); return true; } @@ -235,7 +235,7 @@ throw new ArgumentOutOfRangeException(nameof(high), $"{nameof(high)} ({high}) must be higher than or equal to {nameof(low)} ({low})."); var unsignedMax = System.UInt32.MaxValue; - var range = unchecked((System.UInt32)(high - low + 1)); + var range = unchecked((System.UInt32)(System.UInt32)(high - low + 1)); var intsToReject = range == 0 ? 0 : (unsignedMax - range + 1) % range; return new Int32(low, range, (System.UInt32)intsToReject); @@ -264,7 +264,7 @@ var unsigned = rng.NextUInt32(); if (_range == 0) // 0 is a special case where we sample the entire range. { - result = (System.Int32)unsigned; + result = unchecked((System.Int32)unsigned); return true; } @@ -325,7 +325,7 @@ throw new ArgumentOutOfRangeException(nameof(high), $"{nameof(high)} ({high}) must be higher than or equal to {nameof(low)} ({low})."); var unsignedMax = System.UInt64.MaxValue; - var range = unchecked((System.UInt64)(high - low + 1)); + var range = unchecked((System.UInt64)(System.UInt64)(high - low + 1)); var intsToReject = range == 0 ? 0 : (unsignedMax - range + 1) % range; return new Int64(low, range, (System.UInt64)intsToReject); @@ -354,7 +354,7 @@ var unsigned = rng.NextUInt64(); if (_range == 0) // 0 is a special case where we sample the entire range. { - result = (System.Int64)unsigned; + result = unchecked((System.Int64)unsigned); return true; } @@ -415,7 +415,7 @@ throw new ArgumentOutOfRangeException(nameof(high), $"{nameof(high)} ({high}) must be higher than or equal to {nameof(low)} ({low})."); var unsignedMax = System.UInt32.MaxValue; - var range = unchecked((System.UInt32)(high - low + 1)); + var range = unchecked((System.UInt32)(System.Byte)(high - low + 1)); var intsToReject = range == 0 ? 0 : (unsignedMax - range + 1) % range; return new Byte(low, range, (System.Byte)intsToReject); @@ -444,7 +444,7 @@ var unsigned = rng.NextUInt32(); if (_range == 0) // 0 is a special case where we sample the entire range. { - result = (System.Byte)unsigned; + result = unchecked((System.Byte)unsigned); return true; } @@ -505,7 +505,7 @@ throw new ArgumentOutOfRangeException(nameof(high), $"{nameof(high)} ({high}) must be higher than or equal to {nameof(low)} ({low})."); var unsignedMax = System.UInt32.MaxValue; - var range = unchecked((System.UInt32)(high - low + 1)); + var range = unchecked((System.UInt32)(System.UInt16)(high - low + 1)); var intsToReject = range == 0 ? 0 : (unsignedMax - range + 1) % range; return new UInt16(low, range, (System.UInt16)intsToReject); @@ -534,7 +534,7 @@ var unsigned = rng.NextUInt32(); if (_range == 0) // 0 is a special case where we sample the entire range. { - result = (System.UInt16)unsigned; + result = unchecked((System.UInt16)unsigned); return true; } @@ -595,7 +595,7 @@ throw new ArgumentOutOfRangeException(nameof(high), $"{nameof(high)} ({high}) must be higher than or equal to {nameof(low)} ({low})."); var unsignedMax = System.UInt32.MaxValue; - var range = unchecked((System.UInt32)(high - low + 1)); + var range = unchecked((System.UInt32)(System.UInt32)(high - low + 1)); var intsToReject = range == 0 ? 0 : (unsignedMax - range + 1) % range; return new UInt32(low, range, (System.UInt32)intsToReject); @@ -624,7 +624,7 @@ var unsigned = rng.NextUInt32(); if (_range == 0) // 0 is a special case where we sample the entire range. { - result = (System.UInt32)unsigned; + result = unchecked((System.UInt32)unsigned); return true; } @@ -685,7 +685,7 @@ throw new ArgumentOutOfRangeException(nameof(high), $"{nameof(high)} ({high}) must be higher than or equal to {nameof(low)} ({low})."); var unsignedMax = System.UInt64.MaxValue; - var range = unchecked((System.UInt64)(high - low + 1)); + var range = unchecked((System.UInt64)(System.UInt64)(high - low + 1)); var intsToReject = range == 0 ? 0 : (unsignedMax - range + 1) % range; return new UInt64(low, range, (System.UInt64)intsToReject); @@ -714,7 +714,7 @@ var unsigned = rng.NextUInt64(); if (_range == 0) // 0 is a special case where we sample the entire range. { - result = (System.UInt64)unsigned; + result = unchecked((System.UInt64)unsigned); return true; } diff --git a/src/RandN/Distributions/UniformInt.tt b/src/RandN/Distributions/UniformInt.tt --- a/src/RandN/Distributions/UniformInt.tt +++ b/src/RandN/Distributions/UniformInt.tt @@ -75,7 +75,7 @@ throw new ArgumentOutOfRangeException(nameof(high), $"{nameof(high)} ({high}) must be higher than or equal to {nameof(low)} ({low})."); var unsignedMax = <#= ularge #>.MaxValue; - var range = unchecked((<#= ularge #>)(high - low + 1)); + var range = unchecked((<#= ularge #>)(<#= unsigned #>)(high - low + 1)); var intsToReject = range == 0 ? 0 : (unsignedMax - range + 1) % range; return new <#= shortName #>(low, range, (<#= unsigned #>)intsToReject); @@ -104,7 +104,7 @@ var unsigned = rng.Next<#= nextName #>(); if (_range == 0) // 0 is a special case where we sample the entire range. { - result = (<#= type #>)unsigned; + result = unchecked((<#= type #>)unsigned); return true; }