5 // Bob Smith (bob@thestuff.net)
6 // Ben Maurer (bmaurer@users.sourceforge.net)
7 // Sebastien Pouliot <sebastien@xamarin.com>
9 // (C) 2001 Bob Smith. http://www.thestuff.net
10 // (C) 2003 Ben Maurer
11 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
12 // Copyright 2013 Xamarin Inc. (http://www.xamarin.com)
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 using System.Runtime.InteropServices;
47 : this (Environment.TickCount)
51 public Random (int Seed)
61 x = 314527869 * x + 1234567;
65 ulong t = ((ulong) 4294584393 * z + c);
71 public virtual int Next (int minValue, int maxValue)
73 if (minValue > maxValue)
74 throw new ArgumentOutOfRangeException ("Maximum value is less than minimal value.");
76 // special case: a difference of one (or less) will always return the minimum
77 // e.g. -1,-1 or -1,0 will always return -1
78 uint diff = (uint) (maxValue - minValue);
82 return minValue + ((int) (JKiss () % diff));
85 public virtual int Next (int maxValue)
88 throw new ArgumentOutOfRangeException ("Maximum value is less than minimal value.");
90 return maxValue > 0 ? (int)(JKiss () % maxValue) : 0;
93 public virtual int Next ()
95 // returns a non-negative, [0 - Int32.MacValue], random number
96 // but we want to avoid calls to Math.Abs (call cost and branching cost it requires)
97 // and the fact it would throw for Int32.MinValue (so roughly 1 time out of 2^32)
98 int random = (int) JKiss ();
99 while (random == Int32.MinValue)
100 random = (int) JKiss ();
101 int mask = random >> 31;
103 return random + (mask & 1);
106 public virtual void NextBytes (byte [] buffer)
109 throw new ArgumentNullException ("buffer");
111 // each random `int` can fill 4 bytes
114 for (int i = 0; i < (buffer.Length >> 2); i++) {
116 buffer [p++] = (byte) (random >> 24);
117 buffer [p++] = (byte) (random >> 16);
118 buffer [p++] = (byte) (random >> 8);
119 buffer [p++] = (byte) random;
121 if (p == buffer.Length)
124 // complete the array
126 while (p < buffer.Length) {
127 buffer [p++] = (byte) random;
132 public virtual double NextDouble ()
134 // return a double value between [0,1]
138 protected virtual double Sample ()
140 // a single 32 bits random value is not enough to create a random double value
141 uint a = JKiss () >> 6; // Upper 26 bits
142 uint b = JKiss () >> 5; // Upper 27 bits
143 return (a * 134217728.0 + b) / 9007199254740992.0;