fix the build
[mcs.git] / class / corlib / System / Random.cs
blobeb1d3e83355d99a2aa76b9661dbe70f3ef935e16
1 //
2 // System.Random.cs
3 //
4 // Authors:
5 // Bob Smith (bob@thestuff.net)
6 // Ben Maurer (bmaurer@users.sourceforge.net)
7 //
8 // (C) 2001 Bob Smith. http://www.thestuff.net
9 // (C) 2003 Ben Maurer
13 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
15 // Permission is hereby granted, free of charge, to any person obtaining
16 // a copy of this software and associated documentation files (the
17 // "Software"), to deal in the Software without restriction, including
18 // without limitation the rights to use, copy, modify, merge, publish,
19 // distribute, sublicense, and/or sell copies of the Software, and to
20 // permit persons to whom the Software is furnished to do so, subject to
21 // the following conditions:
22 //
23 // The above copyright notice and this permission notice shall be
24 // included in all copies or substantial portions of the Software.
25 //
26 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
30 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
31 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
32 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 using System.Runtime.InteropServices;
36 namespace System
38 [Serializable]
39 [ComVisible (true)]
40 public class Random
42 const int MBIG = int.MaxValue;
43 const int MSEED = 161803398;
44 const int MZ = 0;
46 int inext, inextp;
47 int [] SeedArray = new int [56];
49 public Random ()
50 : this (Environment.TickCount)
54 public Random (int Seed)
56 int ii;
57 int mj, mk;
59 // Numerical Recipes in C online @ http://www.library.cornell.edu/nr/bookcpdf/c7-1.pdf
60 mj = MSEED - Math.Abs (Seed);
61 SeedArray [55] = mj;
62 mk = 1;
63 for (int i = 1; i < 55; i++) { // [1, 55] is special (Knuth)
64 ii = (21 * i) % 55;
65 SeedArray [ii] = mk;
66 mk = mj - mk;
67 if (mk < 0)
68 mk += MBIG;
69 mj = SeedArray [ii];
71 for (int k = 1; k < 5; k++) {
72 for (int i = 1; i < 56; i++) {
73 SeedArray [i] -= SeedArray [1 + (i + 30) % 55];
74 if (SeedArray [i] < 0)
75 SeedArray [i] += MBIG;
78 inext = 0;
79 inextp = 31;
82 protected virtual double Sample ()
84 int retVal;
86 if (++inext >= 56) inext = 1;
87 if (++inextp >= 56) inextp = 1;
89 retVal = SeedArray [inext] - SeedArray [inextp];
91 if (retVal < 0)
92 retVal += MBIG;
94 SeedArray [inext] = retVal;
96 return retVal * (1.0 / MBIG);
99 public virtual int Next ()
101 return (int)(Sample () * int.MaxValue);
104 public virtual int Next (int maxValue)
106 if (maxValue < 0)
107 throw new ArgumentOutOfRangeException(Locale.GetText (
108 "Max value is less than min value."));
110 return (int)(Sample () * maxValue);
113 public virtual int Next (int minValue, int maxValue)
115 if (minValue > maxValue)
116 throw new ArgumentOutOfRangeException (Locale.GetText (
117 "Min value is greater than max value."));
119 // special case: a difference of one (or less) will always return the minimum
120 // e.g. -1,-1 or -1,0 will always return -1
121 uint diff = (uint) (maxValue - minValue);
122 if (diff <= 1)
123 return minValue;
125 return (int)(Sample () * diff) + minValue;
128 public virtual void NextBytes (byte [] buffer)
130 if (buffer == null)
131 throw new ArgumentNullException ("buffer");
133 for (int i = 0; i < buffer.Length; i++) {
134 buffer [i] = (byte)(Sample () * (byte.MaxValue + 1));
138 public virtual double NextDouble ()
140 return this.Sample ();