2002/06/12 Nick Drochak <ndrochak@gol.com>
[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 using System.Globalization;
12
13 namespace System
14 {
15                 [Serializable]
16         public class Random
17         {
18                 private int S = 1;
19                 private const int A = 16807;
20                 private const int M = 2147483647;
21                 private const int Q = 127773;
22                 private const int R = 2836;
23
24                 public Random()
25                 {
26                         S = (int)(DateTime.Now.Ticks);
27                 }
28
29                 public Random(int Seed)
30                 {
31                         S = Seed;
32                 }
33
34                 public virtual int Next()
35                 {
36                         return (int)(this.Sample()*Int32.MaxValue);
37                 }
38
39                 public virtual int Next(int maxValue)
40                 {
41                         if (maxValue < 0)
42                                 throw new ArgumentOutOfRangeException(Locale.GetText (
43                                         "Max value is less then min value."));
44                         else if (maxValue == 0)
45                                 return 0;
46                         return (int)(this.Sample()*maxValue);
47                 }
48
49                 public virtual int Next(int minValue, int maxValue)
50                 {
51                         if (minValue > maxValue)
52                                 throw new ArgumentOutOfRangeException(Locale.GetText (
53                                         "Min value is greater then max value."));
54                         else if (minValue == maxValue)
55                                 return minValue;
56                         return (int)(this.Sample()*(maxValue - minValue))+minValue;
57                 }
58                 public virtual void NextBytes(byte[] buffer)
59                 {
60                         int i, l;
61                         if (buffer == null)
62                                 throw new ArgumentNullException();
63                         l = buffer.GetUpperBound(0);
64                         for (i = buffer.GetLowerBound(0); i < l; i++)
65                         {
66                                 buffer[i] = (byte)(this.Sample()*Byte.MaxValue);
67                         }
68                 }
69
70                 public virtual double NextDouble ()
71                 {
72                         return this.Sample();
73                 }
74
75                 protected virtual double Sample ()
76                 {
77                         S = A*(S%Q)-R*(S/Q);
78                         if (S < 0)
79                                 S+=M;
80                         return S/(double)Int32.MaxValue;
81                 }
82         }
83 }