//
using System;
+using System.Globalization;
namespace System
{
+ [Serializable]
public class Random
{
private int S = 1;
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;
+ S = (int)(DateTime.Now.Ticks);
}
+
public Random(int Seed)
{
S = Seed;
}
+
public virtual int Next()
{
- return (int)(Random.Sample()*Random.MaxValue);
+ return (int)(this.Sample()*Int32.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);
+ if (maxValue < 0)
+ throw new ArgumentOutOfRangeException(Locale.GetText (
+ "Max value is less then min value."));
+ else if (maxValue == 0)
+ return 0;
+ return (int)(this.Sample()*maxValue);
}
+
public virtual int Next(int minValue, int maxValue)
+ {
if (minValue > maxValue)
- throw new ArgumentOutOfRangeException("Min value is greater then max value.");
+ throw new ArgumentOutOfRangeException(Locale.GetText (
+ "Min value is greater then max value."));
else if (minValue == maxValue)
return minValue;
- return (int)(Random.Sample()*maxValue)+minValue;
+ return (int)(this.Sample()*(maxValue - minValue))+minValue;
}
public virtual void NextBytes(byte[] buffer)
{
int i, l;
- if (buffer == NULL)
- throw ArgumentNullException();
+ if (buffer == null)
+ throw new ArgumentNullException();
l = buffer.GetUpperBound(0);
for (i = buffer.GetLowerBound(0); i < l; i++)
{
- buffer[i] = (byte)(Random.Sample()*Random.MaxValue);
+ buffer[i] = (byte)(this.Sample()*Byte.MaxValue);
}
}
- public virtual double NextDouble()
+
+ public virtual double NextDouble ()
{
- return Random.Sample();
+ return this.Sample();
}
- protected virtual double Sample(){
- S=A*(S%Q)-R*(S/Q);
- if(S<0) S+=M;
+
+ protected virtual double Sample ()
+ {
+ S = A*(S%Q)-R*(S/Q);
+ if (S < 0)
+ S+=M;
return S/(double)Int32.MaxValue;
}
}
}
-