Returns a random int i such that minInt(T) <= i <= maxInt(T).
i is evenly distributed.
pub fn int(r: Random, comptime T: type) T
pub 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);
}