2016-01-03 07:30:41 +00:00
|
|
|
// Mersenne Twister
|
2016-01-23 06:24:09 +00:00
|
|
|
const ARRAY_SIZE = 624;
|
2016-01-03 07:30:41 +00:00
|
|
|
|
2016-04-07 18:34:54 +01:00
|
|
|
/// Use `init` to initialize this state.
|
2016-01-06 11:41:11 +00:00
|
|
|
pub struct Rand {
|
2016-01-10 07:03:31 +00:00
|
|
|
array: [ARRAY_SIZE]u32,
|
2016-01-23 06:24:09 +00:00
|
|
|
index: isize,
|
2016-01-03 07:30:41 +00:00
|
|
|
|
2016-04-07 18:34:54 +01:00
|
|
|
/// Initialize random state with the given seed.
|
2016-04-12 21:30:52 +01:00
|
|
|
#static_eval_enable(false)
|
2016-04-07 18:34:54 +01:00
|
|
|
pub fn init(seed: u32) -> Rand {
|
|
|
|
var r: Rand = undefined;
|
|
|
|
r.index = 0;
|
|
|
|
r.array[0] = seed;
|
|
|
|
var i : isize = 1;
|
2016-05-06 01:19:01 +01:00
|
|
|
var prev_value: u64w = seed;
|
|
|
|
while (i < ARRAY_SIZE; i += 1) {
|
2016-05-09 16:44:41 +01:00
|
|
|
r.array[i] = @truncate(u32, (prev_value ^ (prev_value << 30)) * 0x6c078965 + u64w(i));
|
2016-04-07 18:34:54 +01:00
|
|
|
prev_value = r.array[i];
|
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2016-01-03 07:30:41 +00:00
|
|
|
/// Get 32 bits of randomness.
|
2016-01-26 00:06:19 +00:00
|
|
|
pub fn get_u32(r: &Rand) -> u32 {
|
2016-01-03 07:30:41 +00:00
|
|
|
if (r.index == 0) {
|
|
|
|
r.generate_numbers();
|
|
|
|
}
|
|
|
|
|
|
|
|
// temper the number
|
|
|
|
var y : u32 = r.array[r.index];
|
|
|
|
y ^= y >> 11;
|
|
|
|
y ^= (y >> 7) & 0x9d2c5680;
|
|
|
|
y ^= (y >> 15) & 0xefc60000;
|
|
|
|
y ^= y >> 18;
|
|
|
|
|
|
|
|
r.index = (r.index + 1) % ARRAY_SIZE;
|
|
|
|
return y;
|
|
|
|
}
|
|
|
|
|
2016-01-06 05:47:47 +00:00
|
|
|
/// Fill `buf` with randomness.
|
2016-01-26 00:06:19 +00:00
|
|
|
pub fn get_bytes(r: &Rand, buf: []u8) {
|
2016-01-06 05:47:47 +00:00
|
|
|
var bytes_left = r.get_bytes_aligned(buf);
|
2016-01-03 07:30:41 +00:00
|
|
|
if (bytes_left > 0) {
|
2016-01-26 06:56:46 +00:00
|
|
|
var rand_val_array : [@sizeof(u32)]u8 = undefined;
|
2016-02-07 22:43:19 +00:00
|
|
|
*((&u32)(&rand_val_array[0])) = r.get_u32();
|
2016-01-03 07:30:41 +00:00
|
|
|
while (bytes_left > 0) {
|
2016-01-14 01:15:51 +00:00
|
|
|
buf[buf.len - bytes_left] = rand_val_array[@sizeof(u32) - bytes_left];
|
2016-01-03 07:30:41 +00:00
|
|
|
bytes_left -= 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get a random unsigned integer with even distribution between `start`
|
|
|
|
/// inclusive and `end` exclusive.
|
2016-01-26 00:06:19 +00:00
|
|
|
pub fn range_u64(r: &Rand, start: u64, end: u64) -> u64 {
|
2016-01-03 07:30:41 +00:00
|
|
|
const range = end - start;
|
2016-01-14 01:15:51 +00:00
|
|
|
const leftover = @max_value(u64) % range;
|
|
|
|
const upper_bound = @max_value(u64) - leftover;
|
2016-01-26 06:56:46 +00:00
|
|
|
var rand_val_array : [@sizeof(u64)]u8 = undefined;
|
2016-01-03 07:30:41 +00:00
|
|
|
|
|
|
|
while (true) {
|
2016-01-06 05:47:47 +00:00
|
|
|
r.get_bytes_aligned(rand_val_array);
|
2016-02-07 22:43:19 +00:00
|
|
|
const rand_val = *(&u64)(&rand_val_array[0]);
|
2016-01-03 07:30:41 +00:00
|
|
|
if (rand_val < upper_bound) {
|
|
|
|
return start + (rand_val % range);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-05 11:30:19 +00:00
|
|
|
pub fn float32(r: &Rand) -> f32 {
|
|
|
|
const precision = 16777216;
|
|
|
|
return f32(r.range_u64(0, precision)) / precision;
|
|
|
|
}
|
|
|
|
|
2016-02-07 19:38:51 +00:00
|
|
|
pub fn boolean(r: &Rand) -> bool {
|
|
|
|
return (r.get_u32() & 0x1) == 1;
|
|
|
|
}
|
|
|
|
|
2016-01-26 00:06:19 +00:00
|
|
|
fn generate_numbers(r: &Rand) {
|
2016-02-06 00:15:19 +00:00
|
|
|
for (r.array) |item, i| {
|
2016-01-18 14:04:57 +00:00
|
|
|
const y : u32 = (item & 0x80000000) + (r.array[(i + 1) % ARRAY_SIZE] & 0x7fffffff);
|
2016-01-03 07:30:41 +00:00
|
|
|
const untempered : u32 = r.array[(i + 397) % ARRAY_SIZE] ^ (y >> 1);
|
|
|
|
r.array[i] = if ((y % 2) == 0) {
|
|
|
|
untempered
|
|
|
|
} else {
|
|
|
|
// y is odd
|
|
|
|
untempered ^ 0x9908b0df
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-06 05:47:47 +00:00
|
|
|
// does not populate the remaining (buf.len % 4) bytes
|
2016-01-26 00:06:19 +00:00
|
|
|
fn get_bytes_aligned(r: &Rand, buf: []u8) -> isize {
|
2016-01-06 05:47:47 +00:00
|
|
|
var bytes_left = buf.len;
|
2016-01-06 10:53:30 +00:00
|
|
|
while (bytes_left >= 4) {
|
2016-01-14 09:52:33 +00:00
|
|
|
*((&u32)(&buf[buf.len - bytes_left])) = r.get_u32();
|
2016-01-14 01:15:51 +00:00
|
|
|
bytes_left -= @sizeof(u32);
|
2016-01-03 07:30:41 +00:00
|
|
|
}
|
|
|
|
return bytes_left;
|
|
|
|
}
|
|
|
|
|
2016-01-25 02:27:12 +00:00
|
|
|
}
|
2016-02-05 11:30:19 +00:00
|
|
|
|
|
|
|
#attribute("test")
|
|
|
|
fn test_float32() {
|
2016-02-28 05:06:46 +00:00
|
|
|
var r = Rand.init(42);
|
2016-02-05 11:30:19 +00:00
|
|
|
|
2016-04-20 04:28:44 +01:00
|
|
|
{var i: i32 = 0; while (i < 1000; i += 1) {
|
2016-02-05 11:30:19 +00:00
|
|
|
const val = r.float32();
|
|
|
|
if (!(val >= 0.0)) unreachable{};
|
|
|
|
if (!(val < 1.0)) unreachable{};
|
2016-04-20 04:28:44 +01:00
|
|
|
}}
|
2016-02-05 11:30:19 +00:00
|
|
|
}
|