A single random variate drawn from the uniform distribution between a and b, whose type is the common type of these parameters
auto rnd = Random(unpredictableSeed); // Generate an integer in [0, 1023] auto a = uniform(0, 1024, rnd); assert(0 <= a && a < 1024); // Generate a float in [0, 1) auto b = uniform(0.0f, 1.0f, rnd); assert(0 <= b && b < 1); // Generate a float in [0, 1] b = uniform!"[]"(0.0f, 1.0f, rnd); assert(0 <= b && b <= 1); // Generate a float in (0, 1) b = uniform!"()"(0.0f, 1.0f, rnd); assert(0 < b && b < 1);
Create an array of random numbers using range functions and UFCS
import std.array : array; import std.range : generate, takeExactly; int[] arr = generate!(() => uniform(0, 100)).takeExactly(10).array; assert(arr.length == 10); assert(arr[0] >= 0 && arr[0] < 100);
1 import std.conv : to; 2 import std.meta : AliasSeq; 3 import std.range.primitives : isForwardRange; 4 import std.traits : isIntegral, isSomeChar; 5 6 auto gen = Mt19937(123_456_789); 7 static assert(isForwardRange!(typeof(gen))); 8 9 auto a = uniform(0, 1024, gen); 10 assert(0 <= a && a <= 1024); 11 auto b = uniform(0.0f, 1.0f, gen); 12 assert(0 <= b && b < 1, to!string(b)); 13 auto c = uniform(0.0, 1.0); 14 assert(0 <= c && c < 1); 15 16 static foreach (T; AliasSeq!(char, wchar, dchar, byte, ubyte, short, ushort, 17 int, uint, long, ulong, float, double, real)) 18 {{ 19 T lo = 0, hi = 100; 20 21 // Try tests with each of the possible bounds 22 { 23 T init = uniform(lo, hi); 24 size_t i = 50; 25 while (--i && uniform(lo, hi) == init) {} 26 assert(i > 0); 27 } 28 { 29 T init = uniform!"[)"(lo, hi); 30 size_t i = 50; 31 while (--i && uniform(lo, hi) == init) {} 32 assert(i > 0); 33 } 34 { 35 T init = uniform!"(]"(lo, hi); 36 size_t i = 50; 37 while (--i && uniform(lo, hi) == init) {} 38 assert(i > 0); 39 } 40 { 41 T init = uniform!"()"(lo, hi); 42 size_t i = 50; 43 while (--i && uniform(lo, hi) == init) {} 44 assert(i > 0); 45 } 46 { 47 T init = uniform!"[]"(lo, hi); 48 size_t i = 50; 49 while (--i && uniform(lo, hi) == init) {} 50 assert(i > 0); 51 } 52 53 /* Test case with closed boundaries covering whole range 54 * of integral type 55 */ 56 static if (isIntegral!T || isSomeChar!T) 57 { 58 foreach (immutable _; 0 .. 100) 59 { 60 auto u = uniform!"[]"(T.min, T.max); 61 static assert(is(typeof(u) == T)); 62 assert(T.min <= u, "Lower bound violation for uniform!\"[]\" with " ~ T.stringof); 63 assert(u <= T.max, "Upper bound violation for uniform!\"[]\" with " ~ T.stringof); 64 } 65 } 66 }} 67 68 auto reproRng = Xorshift(239842); 69 70 static foreach (T; AliasSeq!(char, wchar, dchar, byte, ubyte, short, 71 ushort, int, uint, long, ulong)) 72 {{ 73 T lo = T.min + 10, hi = T.max - 10; 74 T init = uniform(lo, hi, reproRng); 75 size_t i = 50; 76 while (--i && uniform(lo, hi, reproRng) == init) {} 77 assert(i > 0); 78 }} 79 80 { 81 bool sawLB = false, sawUB = false; 82 foreach (i; 0 .. 50) 83 { 84 auto x = uniform!"[]"('a', 'd', reproRng); 85 if (x == 'a') sawLB = true; 86 if (x == 'd') sawUB = true; 87 assert('a' <= x && x <= 'd'); 88 } 89 assert(sawLB && sawUB); 90 } 91 92 { 93 bool sawLB = false, sawUB = false; 94 foreach (i; 0 .. 50) 95 { 96 auto x = uniform('a', 'd', reproRng); 97 if (x == 'a') sawLB = true; 98 if (x == 'c') sawUB = true; 99 assert('a' <= x && x < 'd'); 100 } 101 assert(sawLB && sawUB); 102 } 103 104 { 105 bool sawLB = false, sawUB = false; 106 foreach (i; 0 .. 50) 107 { 108 immutable int lo = -2, hi = 2; 109 auto x = uniform!"()"(lo, hi, reproRng); 110 if (x == (lo+1)) sawLB = true; 111 if (x == (hi-1)) sawUB = true; 112 assert(lo < x && x < hi); 113 } 114 assert(sawLB && sawUB); 115 } 116 117 { 118 bool sawLB = false, sawUB = false; 119 foreach (i; 0 .. 50) 120 { 121 immutable ubyte lo = 0, hi = 5; 122 auto x = uniform(lo, hi, reproRng); 123 if (x == lo) sawLB = true; 124 if (x == (hi-1)) sawUB = true; 125 assert(lo <= x && x < hi); 126 } 127 assert(sawLB && sawUB); 128 } 129 130 { 131 foreach (i; 0 .. 30) 132 { 133 assert(i == uniform(i, i+1, reproRng)); 134 } 135 }
Generates a number between a and b. The boundaries parameter controls the shape of the interval (open vs. closed on either side). Valid values for boundaries are "[]", "$(LPAREN)]", "[$(RPAREN)", and "()". The default interval is closed to the left and open to the right. The version that does not take urng uses the default generator rndGen.