5 // Bob Smith (bob@thestuff.net)
6 // Ben Maurer (bmaurer@users.sourceforge.net)
8 // (C) 2001 Bob Smith. http://www.thestuff.net
17 const int MBIG = int.MaxValue;
18 const int MSEED = 161803398;
22 int [] ma = new int [56];
25 : this (Environment.TickCount)
29 public Random (int Seed)
34 // Numerical Recipes in C online @ http://www.library.cornell.edu/nr/bookcpdf/c7-1.pdf
35 mj = MSEED - Math.Abs (Seed);
38 for (int i = 1; i < 55; i++) { // [1, 55] is special (Knuth)
46 for (int k = 1; k < 5; k++) {
47 for (int i = 1; i < 56; i++) {
48 ma [i] -= ma [1 + (i + 30) % 55];
57 protected virtual double Sample ()
61 if (++inext >= 56) inext = 1;
62 if (++inextp >= 56) inextp = 1;
64 retVal = ma [inext] - ma [inextp];
71 return retVal * (1.0 / MBIG);
74 public virtual int Next ()
76 return (int)(Sample () * int.MaxValue);
79 public virtual int Next (int maxValue)
82 throw new ArgumentOutOfRangeException(Locale.GetText (
83 "Max value is less then min value."));
85 return (int)(Sample () * maxValue);
88 public virtual int Next (int minValue, int maxValue)
90 if (minValue > maxValue)
91 throw new ArgumentOutOfRangeException (Locale.GetText (
92 "Min value is greater then max value."));
94 uint diff = (uint)(maxValue - minValue);
98 int result = (int)(Sample () * diff + minValue);
99 return ((result != maxValue) ? result : (result - 1));
102 public virtual void NextBytes (byte [] buffer)
105 throw new ArgumentNullException ("buffer");
107 for (int i = 0; i < buffer.Length; i++) {
108 buffer [i] = (byte)(Sample () * (byte.MaxValue + 1));
112 public virtual double NextDouble ()
114 return this.Sample ();