2 // System.Random Test Cases
5 // Bob Smith <bob@thestuff.net>
6 // Sebastien Pouliot <sebastien@xamarin.com>
8 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
9 // Copyright 2013 Xamarin Inc. (http://www.xamarin.com)
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 using NUnit.Framework;
33 using System.Reflection;
35 namespace MonoTests.System {
38 public class RandomTest {
41 public void CompareStreamWithSameSeed ()
43 Random r = new Random (42);
44 Random r2 = new Random (42);
45 for (int i=0; i<20; i++) {
46 Assert.AreEqual (r.NextDouble (), r2.NextDouble (), i.ToString ());
53 Random r = new Random ();
54 for (int i=0; i<20; i++) {
56 Assert.IsTrue (c < Int32.MaxValue && c >= 0, "Next(" + i + ")");
61 public void NextZero ()
63 Random r = new Random ();
64 Assert.AreEqual (0, r.Next (0),"Next(0) failed");
70 Random r = new Random();
71 for (int i=0; i<20; i++) {
73 Assert.IsTrue (c < 10 && c >= 0, "NextMax(" + i + ")");
78 public void NextMinMax()
80 Random r = new Random ();
81 Assert.AreEqual (42, r.Next (42, 42), "#1 Failed where min == max");
82 Assert.AreEqual (Int32.MaxValue, r.Next (Int32.MaxValue, Int32.MaxValue), "#2 Failed where min == max");
83 Assert.AreEqual (Int32.MinValue, r.Next (Int32.MinValue, Int32.MinValue), "#3 Failed where min == max");
84 Assert.AreEqual (0, r.Next (0, 0), "#4 Failed where min == max");
85 for (int i = 1; i <= Int32.MaxValue / 2; i *= 2) {
86 long c = r.Next (i, i * 2);
87 Assert.IsTrue (c < i * 2, "At i=" + i + " c < i*2 failed");
88 Assert.IsTrue (c >= i, "At i=" + i + " c >= i failed");
90 for (int i = -1; i >= Int32.MinValue / 2; i *= 2) {
91 long c = r.Next (i * 2, i);
92 Assert.IsTrue (c < i, "At i=" + i + " c < i*2 failed");
93 Assert.IsTrue (c >= i * 2, "At i=" + i + " c >= i failed");
97 class RandomSampleOverride : Random {
99 protected override double Sample ()
101 throw new NotImplementedException ();
106 public void Base_Int ()
108 var random = new RandomSampleOverride ();
109 // from 2.0+ Next(), Next(int,int) and NextBytes(byte[]) do not call Sample
110 // see MSDN's Notes to Inheritors
112 random.Next (Int32.MinValue, Int32.MaxValue);
113 random.NextBytes (new byte[1]);
117 [ExpectedException (typeof (NotImplementedException))]
118 public void Base_Double ()
120 var random = new RandomSampleOverride ();
121 random.NextDouble ();
124 // generate values (one for each 1024 returned values) from the original C implementation
125 static uint[] jkiss_values = {
127 1281708802, /* 1024 */
128 1571324528, /* 2048 */
129 1565809406, /* 3072 */
130 1010890569, /* 4096 */
131 1778803435, /* 5120 */
132 903613637, /* 6144 */
133 3496059008, /* 7168 */
134 108603163, /* 8192 */
135 1854081276, /* 9216 */
136 3703232459, /* 10240 */
137 2191562138, /* 11264 */
138 337995793, /* 12288 */
139 1340840062, /* 13312 */
140 2364148985, /* 14336 */
141 2549812361, /* 15360 */
142 563432369, /* 16384 */
143 229365487, /* 17408 */
144 1821397325, /* 18432 */
145 3246092454, /* 19456 */
146 691032417, /* 20480 */
147 86951316, /* 21504 */
148 3029975455, /* 22528 */
149 1261370163, /* 23552 */
150 2539815382, /* 24576 */
151 3017891647, /* 25600 */
152 3877215120, /* 26624 */
153 3142958765, /* 27648 */
154 1080903191, /* 28672 */
155 2837464745, /* 29696 */
156 614275602, /* 30720 */
157 2250626199, /* 31744 */
158 729001311, /* 32768 */
159 3313769017, /* 33792 */
160 2408398670, /* 34816 */
161 3123583383, /* 35840 */
162 3346590423, /* 36864 */
163 1629546563, /* 37888 */
164 251343753, /* 38912 */
165 2695793631, /* 39936 */
166 2768993787, /* 40960 */
167 3688573224, /* 41984 */
168 2897218561, /* 43008 */
169 2725058810, /* 44032 */
170 2142061914, /* 45056 */
171 3983217096, /* 46080 */
172 3609758190, /* 47104 */
173 842060935, /* 48128 */
174 2893482035, /* 49152 */
175 2290461665, /* 50176 */
176 1709481476, /* 51200 */
177 3633857838, /* 52224 */
178 332645044, /* 53248 */
179 3522654497, /* 54272 */
180 2501348469, /* 55296 */
181 1644344287, /* 56320 */
182 3081428084, /* 57344 */
183 3114560766, /* 58368 */
184 489030597, /* 59392 */
185 367291591, /* 60416 */
186 106358682, /* 61440 */
187 3020781303, /* 62464 */
188 1209590375, /* 63488 */
189 1833282169, /* 64512 */
190 61543407, /* 65536 */
196 // Random.Next() returns a non-negative *signed* integer value - so it can't be used for testing
197 var next = typeof(Random).GetMethod ("JKiss", BindingFlags.Instance | BindingFlags.NonPublic);
199 // if the method is not present, e.g. on MS.NET, skip this test
201 Assert.Ignore ("The JKiss method is not present, e.g. on MS.NET.");
203 // ensure we match the original JKISS random stream
204 // first 64KB but without checking every value (one each KB)
205 Random r = new Random (123456789);
208 while (j < 64 * 1024) {
209 uint random = (uint) next.Invoke (r, null);
210 if (j++ % 1024 == 0) {
211 Assert.AreEqual (random, jkiss_values [n], n.ToString ());