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