Random.cs compile fix.
[mono.git] / mcs / class / corlib / System / Random.cs
1 //
2 // System.Random.cs
3 //
4 // Author:
5 //   Bob Smith (bob@thestuff.net)
6 //
7 // (C) 2001 Bob Smith.  http://www.thestuff.net
8 //
9
10 using System;
11
12 namespace System
13 {
14         public class Random
15         {
16                 private int S = 1;
17                 private const int A = 16807;
18                 private const int M = 2147483647;
19                 private const int Q = 127773;
20                 private const int R = 2836;
21                 public const byte MaxValue = 0xFF;
22                 public const byte MinValue = 0x00;
23                 public Random()
24                 {
25                         S = (int)DateTime.Now;
26                 }
27                 public Random(int Seed)
28                 {
29                         S = Seed;
30                 }
31                 public virtual int Next()
32                 {
33                         return (int)(Random.Sample()*Random.MaxValue);
34                 }
35                 public virtual int Next(int maxValue)
36                 {
37                         if (maxValue < Random.MinValue)
38                                 throw new ArgumentOutOfRangeException("Max value is less then min value.");
39                         else if (maxValue == Random.MinValue)
40                                 return Random.MinValue;
41                         return (int)(Random.Sample()*maxValue);
42                 }
43                 public virtual int Next(int minValue, int maxValue)
44                 {
45                         if (minValue > maxValue)
46                                 throw new ArgumentOutOfRangeException("Min value is greater then max value.");
47                         else if (minValue == maxValue)
48                                 return minValue;
49                         return (int)(Random.Sample()*maxValue)+minValue;
50                 }
51                 public virtual void NextBytes(byte[] buffer)
52                 {
53                         int i, l;
54                         if (buffer == NULL)
55                                 throw ArgumentNullException();
56                         l = buffer.GetUpperBound(0);
57                         for (i = buffer.GetLowerBound(0); i < l; i++)
58                         {
59                                 buffer[i] = (byte)(Random.Sample()*Random.MaxValue);
60                         }
61                 }
62                 public virtual double NextDouble()
63                 {
64                         return Random.Sample();
65                 }
66                 protected virtual double Sample(){
67                         S=A*(S%Q)-R*(S/Q);
68                         if(S<0) S+=M;
69                         return S/(double)Int32.MaxValue;
70                 }
71         }
72 }
73