Debian Codegraph @

libphobos/testsuite/libphobos.phobos/std_random.d

raw
1 @safe unittest
2 {
3 import std.random;
4
5 import std.algorithm.comparison : among, equal;
6 import std.range : iota;
7
8 // seed a random generator with a constant
9 auto rnd = Random(42);
10
11 // Generate a uniformly-distributed integer in the range [0, 14]
12 // If no random generator is passed, the global `rndGen` would be used
13 auto i = uniform(0, 15, rnd);
14 assert(i >= 0 && i < 15);
15
16 // Generate a uniformly-distributed real in the range [0, 100)
17 auto r = uniform(0.0L, 100.0L, rnd);
18 assert(r >= 0 && r < 100);
19
20 // Sample from a custom type
21 enum Fruit { apple, mango, pear }
22 auto f = rnd.uniform!Fruit;
23 with(Fruit)
24 assert(f.among(apple, mango, pear));
25
26 // Generate a 32-bit random number
27 auto u = uniform!uint(rnd);
28 static assert(is(typeof(u) == uint));
29
30 // Generate a random number in the range in the range [0, 1)
31 auto u2 = uniform01(rnd);
32 assert(u2 >= 0 && u2 < 1);
33
34 // Select an element randomly
35 auto el = 10.iota.choice(rnd);
36 assert(0 <= el && el < 10);
37
38 // Throw a dice with custom proportions
39 // 0: 20%, 1: 10%, 2: 60%
40 auto val = rnd.dice(0.2, 0.1, 0.6);
41 assert(0 <= val && val <= 2);
42
43 auto rnd2 = MinstdRand0(42);
44
45 // Select a random subsample from a range
46 assert(10.iota.randomSample(3, rnd2).equal([7, 8, 9]));
47
48 // Cover all elements in an array in random order
49 version (D_LP64) // https://issues.dlang.org/show_bug.cgi?id=15147
50 assert(10.iota.randomCover(rnd2).equal([7, 4, 2, 0, 1, 6, 8, 3, 9, 5]));
51 else
52 assert(10.iota.randomCover(rnd2).equal([4, 8, 7, 3, 5, 9, 2, 6, 0, 1]));
53
54 // Shuffle an array
55 version (D_LP64) // https://issues.dlang.org/show_bug.cgi?id=15147
56 assert([0, 1, 2, 4, 5].randomShuffle(rnd2).equal([2, 0, 4, 5, 1]));
57 else
58 assert([0, 1, 2, 4, 5].randomShuffle(rnd2).equal([4, 2, 5, 0, 1]));
59 }
60
61 @safe unittest
62 {
63 import std.random;
64
65 struct NoRng
66 {
67 @property uint front() {return 0;}
68 @property bool empty() {return false;}
69 void popFront() {}
70 }
71 static assert(!isUniformRNG!(NoRng));
72
73 struct validRng
74 {
75 @property uint front() {return 0;}
76 @property bool empty() {return false;}
77 void popFront() {}
78
79 enum isUniformRandom = true;
80 }
81 static assert(isUniformRNG!(validRng, uint));
82 static assert(isUniformRNG!(validRng));
83 }
84
85 @safe unittest
86 {
87 import std.random;
88
89 struct validRng
90 {
91 @property uint front() {return 0;}
92 @property bool empty() {return false;}
93 void popFront() {}
94
95 enum isUniformRandom = true;
96 }
97 static assert(!isSeedable!(validRng, uint));
98 static assert(!isSeedable!(validRng));
99
100 struct seedRng
101 {
102 @property uint front() {return 0;}
103 @property bool empty() {return false;}
104 void popFront() {}
105 void seed(uint val){}
106 enum isUniformRandom = true;
107 }
108 static assert(isSeedable!(seedRng, uint));
109 static assert(!isSeedable!(seedRng, ulong));
110 static assert(isSeedable!(seedRng));
111 }
112
113 @safe unittest
114 {
115 import std.random;
116
117 alias CPP11LCG = LinearCongruentialEngine!(uint, 48271, 0, 2_147_483_647);
118
119 // seed with a constant
120 auto rnd = CPP11LCG(42);
121 auto n = rnd.front; // same for each run
122 assert(n == 2027382);
123 }
124
125 @safe unittest
126 {
127 import std.random;
128
129 // glibc's LCG
130 alias GLibcLCG = LinearCongruentialEngine!(uint, 1103515245, 12345, 2_147_483_648);
131
132 // Seed with an unpredictable value
133 auto rnd = GLibcLCG(unpredictableSeed);
134 auto n = rnd.front; // different across runs
135 }
136
137 @safe unittest
138 {
139 import std.random;
140
141 // Visual C++'s LCG
142 alias MSVCLCG = LinearCongruentialEngine!(uint, 214013, 2531011, 0);
143
144 // seed with a constant
145 auto rnd = MSVCLCG(1);
146 auto n = rnd.front; // same for each run
147 assert(n == 2745024);
148 }
149
150 @safe @nogc unittest
151 {
152 import std.random;
153
154 // seed with a constant
155 auto rnd0 = MinstdRand0(1);
156 auto n = rnd0.front;
157 // same for each run
158 assert(n == 16807);
159
160 // Seed with an unpredictable value
161 rnd0.seed(unpredictableSeed);
162 n = rnd0.front; // different across runs
163 }
164
165 @safe unittest
166 {
167 import std.random;
168
169 // seed with a constant
170 Mt19937 gen;
171 auto n = gen.front; // same for each run
172 assert(n == 3499211612);
173
174 // Seed with an unpredictable value
175 gen.seed(unpredictableSeed);
176 n = gen.front; // different across runs
177 }
178
179 @safe @nogc unittest
180 {
181 import std.random;
182
183 // seed with a constant
184 Mt19937 gen;
185 auto n = gen.front; // same for each run
186 assert(n == 3499211612);
187
188 // Seed with an unpredictable value
189 gen.seed(unpredictableSeed);
190 n = gen.front; // different across runs
191 }
192
193 @safe @nogc unittest
194 {
195 import std.random;
196
197 // Seed with a constant
198 auto gen = Mt19937_64(12345);
199 auto n = gen.front; // same for each run
200 assert(n == 6597103971274460346);
201
202 // Seed with an unpredictable value
203 gen.seed(unpredictableSeed!ulong);
204 n = gen.front; // different across runs
205 }
206
207 @safe unittest
208 {
209 import std.random;
210
211 alias Xorshift96 = XorshiftEngine!(uint, 96, 10, 5, 26);
212 auto rnd = Xorshift96(42);
213 auto num = rnd.front; // same for each run
214 assert(num == 2704588748);
215 }
216
217 @safe @nogc unittest
218 {
219 import std.random;
220
221 // Seed with a constant
222 auto rnd = Xorshift(1);
223 auto num = rnd.front; // same for each run
224 assert(num == 1405313047);
225
226 // Seed with an unpredictable value
227 rnd.seed(unpredictableSeed);
228 num = rnd.front; // different across rnd
229 }
230
231 @safe @nogc unittest
232 {
233 import std.random;
234
235 auto rnd = Random(unpredictableSeed);
236 auto n = rnd.front;
237 static assert(is(typeof(n) == uint));
238 }
239
240 @safe nothrow @nogc unittest
241 {
242 import std.random;
243
244 import std.algorithm.iteration : sum;
245 import std.range : take;
246 auto rnd = rndGen;
247 assert(rnd.take(3).sum > 0);
248 }
249
250 @safe unittest
251 {
252 import std.random;
253
254 auto rnd = Random(unpredictableSeed);
255
256 // Generate an integer in [0, 1023]
257 auto a = uniform(0, 1024, rnd);
258 assert(0 <= a && a < 1024);
259
260 // Generate a float in [0, 1)
261 auto b = uniform(0.0f, 1.0f, rnd);
262 assert(0 <= b && b < 1);
263
264 // Generate a float in [0, 1]
265 b = uniform!"[]"(0.0f, 1.0f, rnd);
266 assert(0 <= b && b <= 1);
267
268 // Generate a float in (0, 1)
269 b = uniform!"()"(0.0f, 1.0f, rnd);
270 assert(0 < b && b < 1);
271 }
272
273 @safe unittest
274 {
275 import std.random;
276
277 import std.array : array;
278 import std.range : generate, takeExactly;
279
280 int[] arr = generate!(() => uniform(0, 100)).takeExactly(10).array;
281 assert(arr.length == 10);
282 assert(arr[0] >= 0 && arr[0] < 100);
283 }
284
285 @safe unittest
286 {
287 import std.random;
288
289 import std.conv : to;
290 import std.meta : AliasSeq;
291 import std.range.primitives : isForwardRange;
292 import std.traits : isIntegral, isSomeChar;
293
294 auto gen = Mt19937(123_456_789);
295 static assert(isForwardRange!(typeof(gen)));
296
297 auto a = uniform(0, 1024, gen);
298 assert(0 <= a && a <= 1024);
299 auto b = uniform(0.0f, 1.0f, gen);
300 assert(0 <= b && b < 1, to!string(b));
301 auto c = uniform(0.0, 1.0);
302 assert(0 <= c && c < 1);
303
304 static foreach (T; AliasSeq!(char, wchar, dchar, byte, ubyte, short, ushort,
305 int, uint, long, ulong, float, double, real))
306 {{
307 T lo = 0, hi = 100;
308
309 // Try tests with each of the possible bounds
310 {
311 T init = uniform(lo, hi);
312 size_t i = 50;
313 while (--i && uniform(lo, hi) == init) {}
314 assert(i > 0);
315 }
316 {
317 T init = uniform!"[)"(lo, hi);
318 size_t i = 50;
319 while (--i && uniform(lo, hi) == init) {}
320 assert(i > 0);
321 }
322 {
323 T init = uniform!"(]"(lo, hi);
324 size_t i = 50;
325 while (--i && uniform(lo, hi) == init) {}
326 assert(i > 0);
327 }
328 {
329 T init = uniform!"()"(lo, hi);
330 size_t i = 50;
331 while (--i && uniform(lo, hi) == init) {}
332 assert(i > 0);
333 }
334 {
335 T init = uniform!"[]"(lo, hi);
336 size_t i = 50;
337 while (--i && uniform(lo, hi) == init) {}
338 assert(i > 0);
339 }
340
341 /* Test case with closed boundaries covering whole range
342 * of integral type
343 */
344 static if (isIntegral!T || isSomeChar!T)
345 {
346 foreach (immutable _; 0 .. 100)
347 {
348 auto u = uniform!"[]"(T.min, T.max);
349 static assert(is(typeof(u) == T));
350 assert(T.min <= u, "Lower bound violation for uniform!\"[]\" with " ~ T.stringof);
351 assert(u <= T.max, "Upper bound violation for uniform!\"[]\" with " ~ T.stringof);
352 }
353 }
354 }}
355
356 auto reproRng = Xorshift(239842);
357
358 static foreach (T; AliasSeq!(char, wchar, dchar, byte, ubyte, short,
359 ushort, int, uint, long, ulong))
360 {{
361 T lo = T.min + 10, hi = T.max - 10;
362 T init = uniform(lo, hi, reproRng);
363 size_t i = 50;
364 while (--i && uniform(lo, hi, reproRng) == init) {}
365 assert(i > 0);
366 }}
367
368 {
369 bool sawLB = false, sawUB = false;
370 foreach (i; 0 .. 50)
371 {
372 auto x = uniform!"[]"('a', 'd', reproRng);
373 if (x == 'a') sawLB = true;
374 if (x == 'd') sawUB = true;
375 assert('a' <= x && x <= 'd');
376 }
377 assert(sawLB && sawUB);
378 }
379
380 {
381 bool sawLB = false, sawUB = false;
382 foreach (i; 0 .. 50)
383 {
384 auto x = uniform('a', 'd', reproRng);
385 if (x == 'a') sawLB = true;
386 if (x == 'c') sawUB = true;
387 assert('a' <= x && x < 'd');
388 }
389 assert(sawLB && sawUB);
390 }
391
392 {
393 bool sawLB = false, sawUB = false;
394 foreach (i; 0 .. 50)
395 {
396 immutable int lo = -2, hi = 2;
397 auto x = uniform!"()"(lo, hi, reproRng);
398 if (x == (lo+1)) sawLB = true;
399 if (x == (hi-1)) sawUB = true;
400 assert(lo < x && x < hi);
401 }
402 assert(sawLB && sawUB);
403 }
404
405 {
406 bool sawLB = false, sawUB = false;
407 foreach (i; 0 .. 50)
408 {
409 immutable ubyte lo = 0, hi = 5;
410 auto x = uniform(lo, hi, reproRng);
411 if (x == lo) sawLB = true;
412 if (x == (hi-1)) sawUB = true;
413 assert(lo <= x && x < hi);
414 }
415 assert(sawLB && sawUB);
416 }
417
418 {
419 foreach (i; 0 .. 30)
420 {
421 assert(i == uniform(i, i+1, reproRng));
422 }
423 }
424 }
425
426 @safe unittest
427 {
428 import std.random;
429
430 auto rnd = MinstdRand0(42);
431
432 assert(rnd.uniform!ubyte == 102);
433 assert(rnd.uniform!ulong == 4838462006927449017);
434
435 enum Fruit { apple, mango, pear }
436 version (D_LP64) // https://issues.dlang.org/show_bug.cgi?id=15147
437 assert(rnd.uniform!Fruit == Fruit.mango);
438 }
439
440 @safe @nogc unittest
441 {
442 import std.random;
443
444 import std.math.operations : feqrel;
445
446 auto rnd = MinstdRand0(42);
447
448 // Generate random numbers in the range in the range [0, 1)
449 auto u1 = uniform01(rnd);
450 assert(u1 >= 0 && u1 < 1);
451
452 auto u2 = rnd.uniform01!float;
453 assert(u2 >= 0 && u2 < 1);
454
455 // Confirm that the random values with the initial seed 42 are 0.000328707 and 0.524587
456 assert(u1.feqrel(0.000328707) > 20);
457 assert(u2.feqrel(0.524587) > 20);
458 }
459
460 @safe unittest
461 {
462 import std.random;
463
464 import std.algorithm.iteration : reduce;
465 import std.math.operations : isClose;
466
467 auto a = uniformDistribution(5);
468 assert(a.length == 5);
469 assert(isClose(reduce!"a + b"(a), 1));
470
471 a = uniformDistribution(10, a);
472 assert(a.length == 10);
473 assert(isClose(reduce!"a + b"(a), 1));
474 }
475
476 @safe unittest
477 {
478 import std.random;
479
480 auto rnd = MinstdRand0(42);
481
482 auto elem = [1, 2, 3, 4, 5].choice(rnd);
483 version (D_LP64) // https://issues.dlang.org/show_bug.cgi?id=15147
484 assert(elem == 3);
485 }
486
487 @safe unittest
488 {
489 import std.random;
490
491 auto rnd = MinstdRand0(42);
492
493 auto arr = [1, 2, 3, 4, 5].randomShuffle(rnd);
494 version (D_LP64) // https://issues.dlang.org/show_bug.cgi?id=15147
495 assert(arr == [3, 5, 2, 4, 1]);
496 }
497
498 @safe unittest
499 {
500 import std.random;
501
502 auto rnd = MinstdRand0(42);
503
504 auto arr = [1, 2, 3, 4, 5, 6];
505 arr = arr.dup.partialShuffle(1, rnd);
506
507 version (D_LP64) // https://issues.dlang.org/show_bug.cgi?id=15147
508 assert(arr == [2, 1, 3, 4, 5, 6]); // 1<->2
509
510 arr = arr.dup.partialShuffle(2, rnd);
511 version (D_LP64) // https://issues.dlang.org/show_bug.cgi?id=15147
512 assert(arr == [1, 4, 3, 2, 5, 6]); // 1<->2, 2<->4
513
514 arr = arr.dup.partialShuffle(3, rnd);
515 version (D_LP64) // https://issues.dlang.org/show_bug.cgi?id=15147
516 assert(arr == [5, 4, 6, 2, 1, 3]); // 1<->5, 2<->4, 3<->6
517 }
518
519 @safe unittest
520 {
521 import std.random;
522
523 auto d6 = 1 + dice(1, 1, 1, 1, 1, 1); // fair dice roll
524 auto d6b = 1 + dice(2, 1, 1, 1, 1, 1); // double the chance to roll '1'
525
526 auto x = dice(0.5, 0.5); // x is 0 or 1 in equal proportions
527 auto y = dice(50, 50); // y is 0 or 1 in equal proportions
528 auto z = dice(70, 20, 10); // z is 0 70% of the time, 1 20% of the time,
529 // and 2 10% of the time
530 }
531
532 @safe unittest
533 {
534 import std.random;
535
536 auto rnd = MinstdRand0(42);
537 auto z = rnd.dice(70, 20, 10);
538 assert(z == 0);
539 z = rnd.dice(30, 20, 40, 10);
540 assert(z == 2);
541 }
542
543 @safe unittest
544 {
545 import std.random;
546
547 auto rnd = Xorshift(123_456_789);
548 auto i = dice(rnd, 0.0, 100.0);
549 assert(i == 1);
550 i = dice(rnd, 100.0, 0.0);
551 assert(i == 0);
552
553 i = dice(100U, 0U);
554 assert(i == 0);
555 }
556
557 @safe unittest
558 {
559 import std.random;
560
561 import std.algorithm.comparison : equal;
562 import std.range : iota;
563 auto rnd = MinstdRand0(42);
564
565 version (D_LP64) // https://issues.dlang.org/show_bug.cgi?id=15147
566 assert(10.iota.randomCover(rnd).equal([7, 4, 2, 0, 1, 6, 8, 3, 9, 5]));
567 }
568
569 @safe unittest
570 {
571 import std.random;
572
573 import std.algorithm.comparison : equal;
574 import std.range : iota;
575 auto rnd = MinstdRand0(42);
576 assert(10.iota.randomSample(3, rnd).equal([7, 8, 9]));
577 }
578