Fix to UriTemplate.Match to properly handle query parameters without a value. No...
[mono.git] / mcs / class / corlib / System / Random.cs
1 //
2 // System.Random.cs
3 //
4 // Authors:
5 //   Bob Smith (bob@thestuff.net)
6 //   Ben Maurer (bmaurer@users.sourceforge.net)
7 //   Sebastien Pouliot  <sebastien@xamarin.com>
8 //
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)
13 //
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:
21 // 
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
24 // 
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.
32 //
33 using System.Runtime.InteropServices;
34
35 namespace System
36 {
37         [Serializable]
38         [ComVisible (true)]
39         public class Random
40         {
41                 uint x;
42                 uint y;
43                 uint z;
44                 uint c;
45
46                 public Random ()
47                         : this (Environment.TickCount)
48                 {
49                 }
50
51                 public Random (int Seed)
52                 {
53                         x = (uint) Seed;
54                         y = (uint) 987654321;
55                         z = (uint) 43219876;
56                         c = (uint) 6543217;
57                 }
58
59                 uint JKiss ()
60                 {
61                         x = 314527869 * x + 1234567;
62                         y ^= y << 5;
63                         y ^= y >> 7;
64                         y ^= y << 22;
65                         ulong t = ((ulong) 4294584393 * z + c);
66                         c = (uint) (t >> 32);
67                         z = (uint) t;
68                         return (x + y + z);
69                 }
70
71                 public virtual int Next (int minValue, int maxValue)
72                 {
73                         if (minValue > maxValue)
74                                 throw new ArgumentOutOfRangeException ("Maximum value is less than minimal value.");
75
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);
79                         if (diff <= 1)
80                                 return minValue;
81
82                         return minValue + ((int) (JKiss () % diff));
83                 }
84
85                 public virtual int Next (int maxValue)
86                 {
87                         if (maxValue < 0)
88                                 throw new ArgumentOutOfRangeException ("Maximum value is less than minimal value.");
89
90                         return maxValue > 0 ? (int)(JKiss () % maxValue) : 0;
91                 }
92
93                 public virtual int Next ()
94                 {
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;
102                         random ^= mask;
103                         return random + (mask & 1);
104                 }
105
106                 public virtual void NextBytes (byte [] buffer)
107                 {
108                         if (buffer == null)
109                                 throw new ArgumentNullException ("buffer");
110
111                         // each random `int` can fill 4 bytes
112                         int p = 0;
113                         uint random;
114                         for (int i = 0; i < (buffer.Length >> 2); i++) {
115                                 random = JKiss ();
116                                 buffer [p++] = (byte) (random >> 24);
117                                 buffer [p++] = (byte) (random >> 16);
118                                 buffer [p++] = (byte) (random >> 8);
119                                 buffer [p++] = (byte) random;
120                         }
121                         if (p == buffer.Length)
122                                 return;
123
124                         // complete the array
125                         random = JKiss ();
126                         while (p < buffer.Length) {
127                                 buffer [p++] = (byte) random;
128                                 random >>= 8;
129                         }
130                 }
131
132                 public virtual double NextDouble ()
133                 {
134                         // return a double value between [0,1]
135                         return Sample ();
136                 }
137
138                 protected virtual double Sample ()
139                 {
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;
144                 }
145         }
146 }