From e34947c627d5f4a17815ca6fef81d959c32bcbe0 Mon Sep 17 00:00:00 2001 From: Christoph Ruegg Date: Mon, 14 Jun 2010 22:30:33 +0200 Subject: [PATCH] fixing bug in abstract RNG base for range-limited integers --- .../Random/AbstractRandomNumberGenerator.cs | 24 +++++++++++-------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/src/Numerics/Random/AbstractRandomNumberGenerator.cs b/src/Numerics/Random/AbstractRandomNumberGenerator.cs index 8fdf3fb4..7d698eb0 100644 --- a/src/Numerics/Random/AbstractRandomNumberGenerator.cs +++ b/src/Numerics/Random/AbstractRandomNumberGenerator.cs @@ -3,7 +3,9 @@ // http://numerics.mathdotnet.com // http://github.com/mathnet/mathnet-numerics // http://mathnetnumerics.codeplex.com +// // Copyright (c) 2009-2010 Math.NET +// // Permission is hereby granted, free of charge, to any person // obtaining a copy of this software and associated documentation // files (the "Software"), to deal in the Software without @@ -12,8 +14,10 @@ // copies of the Software, and to permit persons to whom the // Software is furnished to do so, subject to the following // conditions: +// // The above copyright notice and this permission notice shall be // included in all copies or substantial portions of the Software. +// // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND @@ -68,7 +72,7 @@ namespace MathNet.Numerics.Random /// safe classes. protected AbstractRandomNumberGenerator(bool threadSafe) { - this._sampleMethod = threadSafe ? (SampleMethod)this.ThreadSafeSample : this.DoSample; + _sampleMethod = threadSafe ? (SampleMethod)ThreadSafeSample : DoSample; } /// @@ -89,7 +93,7 @@ namespace MathNet.Numerics.Random var ret = new double[n]; for (var i = 0; i < ret.Length; i++) { - ret[i] = this.Sample(); + ret[i] = Sample(); } return ret; @@ -103,7 +107,7 @@ namespace MathNet.Numerics.Random /// public override int Next() { - return (int)(this.Sample() * int.MaxValue); + return (int)(Sample() * int.MaxValue); } /// @@ -114,12 +118,12 @@ namespace MathNet.Numerics.Random /// is negative. public override int Next(int maxValue) { - if (0 > maxValue) + if (maxValue <= 0) { throw new ArgumentOutOfRangeException(Resources.ArgumentMustBePositive); } - return (int)(this.Sample() % maxValue); + return (int)(Sample() * maxValue); } /// @@ -138,7 +142,7 @@ namespace MathNet.Numerics.Random throw new ArgumentOutOfRangeException(Resources.ArgumentMinValueGreaterThanMaxValue); } - return (int)(this.Sample() * (maxValue - minValue)) + minValue; + return (int)(Sample() * (maxValue - minValue)) + minValue; } /// @@ -155,7 +159,7 @@ namespace MathNet.Numerics.Random for (var i = 0; i < buffer.Length; i++) { - buffer[i] = (byte)(this.Next() % 256); + buffer[i] = (byte)(Next() % 256); } } @@ -165,7 +169,7 @@ namespace MathNet.Numerics.Random /// A double-precision floating point number greater than or equal to 0.0, and less than 1.0. protected override double Sample() { - return this._sampleMethod(); + return _sampleMethod(); } /// @@ -174,9 +178,9 @@ namespace MathNet.Numerics.Random /// A double-precision floating point number greater than or equal to 0.0, and less than 1.0 private double ThreadSafeSample() { - lock (this._lock) + lock (_lock) { - return this.DoSample(); + return DoSample(); } }