28df260b85fd2148f060762b186a8cd8dd0ebaf4
[mono.git] / mcs / class / Mono.C5 / current / C5 / Random.cs
1 /*\r
2  Copyright (c) 2003-2006 Niels Kokholm and Peter Sestoft\r
3  Permission is hereby granted, free of charge, to any person obtaining a copy\r
4  of this software and associated documentation files (the "Software"), to deal\r
5  in the Software without restriction, including without limitation the rights\r
6  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\r
7  copies of the Software, and to permit persons to whom the Software is\r
8  furnished to do so, subject to the following conditions:\r
9  \r
10  The above copyright notice and this permission notice shall be included in\r
11  all copies or substantial portions of the Software.\r
12  \r
13  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
14  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\r
15  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\r
16  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\r
17  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\r
18  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\r
19  SOFTWARE.\r
20 */\r
21 using System;\r
22 using System.Diagnostics;\r
23 using SCG = System.Collections.Generic;\r
24 namespace C5\r
25 {\r
26   /// <summary>\r
27   /// A modern random number generator based on G. Marsaglia: \r
28   /// Seeds for Random Number Generators, Communications of the\r
29   /// ACM 46, 5 (May 2003) 90-93; and a posting by Marsaglia to \r
30   /// comp.lang.c on 2003-04-03.\r
31   /// </summary>\r
32   public class C5Random : Random\r
33   {\r
34     private uint[] Q = new uint[16];\r
35 \r
36     private uint c = 362436, i = 15;\r
37 \r
38 \r
39     private uint Cmwc()\r
40     {\r
41       ulong t, a = 487198574UL;\r
42       uint x, r = 0xfffffffe;\r
43 \r
44       i = (i + 1) & 15;\r
45       t = a * Q[i] + c;\r
46       c = (uint)(t >> 32);\r
47       x = (uint)(t + c);\r
48       if (x < c)\r
49       {\r
50         x++;\r
51         c++;\r
52       }\r
53 \r
54       return Q[i] = r - x;\r
55     }\r
56 \r
57 \r
58     /// <summary>\r
59     /// Get a new random System.Double value\r
60     /// </summary>\r
61     /// <returns>The random double</returns>\r
62     public override double NextDouble()\r
63     {\r
64       return Cmwc() / 4294967296.0;\r
65     }\r
66 \r
67 \r
68     /// <summary>\r
69     /// Get a new random System.Double value\r
70     /// </summary>\r
71     /// <returns>The random double</returns>\r
72     protected override double Sample()\r
73     {\r
74       return NextDouble();\r
75     }\r
76 \r
77 \r
78     /// <summary>\r
79     /// Get a new random System.Int32 value\r
80     /// </summary>\r
81     /// <returns>The random int</returns>\r
82     public override int Next()\r
83     {\r
84       return (int)Cmwc();\r
85     }\r
86 \r
87 \r
88     /// <summary>\r
89     /// Get a random non-negative integer less than a given upper bound\r
90     /// </summary>\r
91     /// <exception cref="ArgumentException">If max is negative</exception>\r
92     /// <param name="max">The upper bound (exclusive)</param>\r
93     /// <returns></returns>\r
94     public override int Next(int max)\r
95     {\r
96       if (max < 0)\r
97         throw new ArgumentException("max must be non-negative");\r
98 \r
99       return (int)(Cmwc() / 4294967296.0 * max);\r
100     }\r
101 \r
102 \r
103     /// <summary>\r
104     /// Get a random integer between two given bounds\r
105     /// </summary>\r
106     /// <exception cref="ArgumentException">If max is less than min</exception>\r
107     /// <param name="min">The lower bound (inclusive)</param>\r
108     /// <param name="max">The upper bound (exclusive)</param>\r
109     /// <returns></returns>\r
110     public override int Next(int min, int max)\r
111     {\r
112       if (min > max)\r
113         throw new ArgumentException("min must be less than or equal to max");\r
114 \r
115       return min + (int)(Cmwc() / 4294967296.0 * (max - min));\r
116     }\r
117 \r
118     /// <summary>\r
119     /// Fill a array of byte with random bytes\r
120     /// </summary>\r
121     /// <param name="buffer">The array to fill</param>\r
122     public override void NextBytes(byte[] buffer)\r
123     {\r
124       for (int i = 0, length = buffer.Length; i < length; i++)\r
125         buffer[i] = (byte)Cmwc();\r
126     }\r
127 \r
128 \r
129     /// <summary>\r
130     /// Create a random number generator seed by system time.\r
131     /// </summary>\r
132     public C5Random() : this(DateTime.Now.Ticks)\r
133     {\r
134     }\r
135 \r
136 \r
137     /// <summary>\r
138     /// Create a random number generator with a given seed\r
139     /// </summary>\r
140     /// <exception cref="ArgumentException">If seed is zero</exception>\r
141     /// <param name="seed">The seed</param>\r
142     public C5Random(long seed)\r
143     {\r
144       if (seed == 0)\r
145         throw new ArgumentException("Seed must be non-zero");\r
146 \r
147       uint j = (uint)(seed & 0xFFFFFFFF);\r
148 \r
149       for (int i = 0; i < 16; i++)\r
150       {\r
151         j ^= j << 13;\r
152         j ^= j >> 17;\r
153         j ^= j << 5;\r
154         Q[i] = j;\r
155       }\r
156 \r
157       Q[15] = (uint)(seed ^ (seed >> 32));\r
158     }\r
159 \r
160     /// <summary>\r
161     /// Create a random number generator with a specified internal start state.\r
162     /// </summary>\r
163     /// <exception cref="ArgumentException">If Q is not of length exactly 16</exception>\r
164     /// <param name="Q">The start state. Must be a collection of random bits given by an array of exactly 16 uints.</param>\r
165     public C5Random(uint[] Q)\r
166     {\r
167       if (Q.Length != 16)\r
168         throw new ArgumentException("Q must have length 16, was " + Q.Length);\r
169       Array.Copy(Q, this.Q, 16);\r
170     }\r
171   }\r
172 }