Implemented the System.Random class. I dont have a C# compiler, so someone will need...
authorBob Smith <bobsmith@mono-cvs.ximian.com>
Thu, 2 Aug 2001 23:04:31 +0000 (23:04 -0000)
committerBob Smith <bobsmith@mono-cvs.ximian.com>
Thu, 2 Aug 2001 23:04:31 +0000 (23:04 -0000)
svn path=/trunk/mcs/; revision=390

mcs/AUTHORS
mcs/class/corlib/System/ChangeLog
mcs/class/corlib/System/Random.cs [new file with mode: 0644]

index b092bcf9334d54ba0c57c5a676df42a89075f447..66b994d3f6118774bd24f8a9b58b17640caccb65 100755 (executable)
@@ -8,3 +8,4 @@ Class Libraries:
        Sean MacIsaac (macisaac@ximian.com)
        Vladimir Vukicevic (vladimir@ximian.com)
        Garrett Rooney (rooneg@electricjellyfish.net)
+        Bob Smith (bob@thestuff.net)
index e437871a0805755f9f55701eab77a0f07f9e1d2a..cc44b51dbcd5cde8b722f33005e0d942e8fe8317 100644 (file)
@@ -1,3 +1,7 @@
+2001-08-02  Bob Smith  <bob@thestuff.net>
+
+        * Random.cs: Implemented. Needs testing.
+
 2001-08-02  Miguel de Icaza  <miguel@ximian.com>
 
        * IServiceProvider.cs, EventHandler.cs: New files.
diff --git a/mcs/class/corlib/System/Random.cs b/mcs/class/corlib/System/Random.cs
new file mode 100644 (file)
index 0000000..256762d
--- /dev/null
@@ -0,0 +1,72 @@
+//
+// System.Random.cs
+//
+// Author:
+//   Bob Smith (bob@thestuff.net)
+//
+// (C) 2001 Bob Smith.  http://www.thestuff.net
+//
+
+using System;
+
+namespace System
+{
+        public class Random
+        {
+                private int S = 1;
+                private const int A = 16807;
+                private const int M = 2147483647;
+                private const int Q = 127773;
+                private const int R = 2836;
+                public const byte MaxValue = 0xFF;
+                public const byte MinValue = 0x00;
+                public Random()
+                {
+                        S = (int)DateTime.Now;
+                }
+                public Random(int Seed)
+                {
+                        S = Seed;
+                }
+                public virtual int Next()
+                {
+                        return (int)(Random.Sample()*Random.MaxValue);
+                }
+                public virtual int Next(int maxValue)
+                {
+                        if (maxValue < Random.MinValue)
+                                throw new ArgumentOutOfRangeException("Max value is less then min value.");
+                        else if (maxValue == Random.MinValue)
+                                return Random.MinValue;
+                        return (int)(Random.Sample()*maxValue);
+                }
+                public virtual int Next(int minValue, int maxValue)
+                        if (minValue > maxValue)
+                                throw new ArgumentOutOfRangeException("Min value is greater then max value.");
+                        else if (minValue == maxValue)
+                                return minValue;
+                        return (int)(Random.Sample()*maxValue)+minValue;
+                }
+                public virtual void NextBytes(byte[] buffer)
+                {
+                        int i, l;
+                        if (buffer == NULL)
+                                throw ArgumentNullException();
+                        l = buffer.GetUpperBound(0);
+                        for (i = buffer.GetLowerBound(0); i < l; i++)
+                        {
+                                buffer[i] = (byte)(Random.Sample()*Random.MaxValue);
+                        }
+                }
+                public virtual double NextDouble()
+                {
+                        return Random.Sample();
+                }
+                protected virtual double Sample(){
+                        S=A*(S%Q)-R*(S/Q);
+                        if(S<0) S+=M;
+                        return S/(double)Int32.MaxValue;
+                }
+        }
+}
+