pub fn int(r: Random, comptime T: type) T r: RandomT: type126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141pub fn int(r: Random, comptime T: type) T {
const bits = @typeInfo(T).int.bits;
const UnsignedT = @Int(.unsigned, bits);
const ceil_bytes = @divCeil(bits, 8);
const ByteAlignedT = @Int(.unsigned, ceil_bytes * 8);
var rand_bytes: [ceil_bytes]u8 = undefined;
r.bytes(&rand_bytes);
// use LE instead of native endian for better portability maybe?
// TODO: endian portability is pointless if the underlying prng isn't endian portable.
// TODO: document the endian portability of this library.
const byte_aligned_result = mem.readInt(ByteAlignedT, &rand_bytes, .little);
const unsigned_result: UnsignedT = @truncate(byte_aligned_result);
return @bitCast(unsigned_result);
}
Returns a random int
isuch thatminInt(T) <= i <= maxInt(T).iis evenly distributed.