authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-07-27 19:52:38-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-07-27 19:52:38-07:00
log3eb5afd245d8e2059a8cf4b8aa4e201ec0613535
tree24e8d22f5ec77b43742c41ff3854e6ca2056f5dd
parent06c4b35eb12a54a4e260a80ed8ed21eb5bfae09a

std: cleanup of rand


1 files changed, 7 insertions(+), 9 deletions(-)

std/rand.zig+7-9
......@@ -34,9 +34,9 @@ pub struct Rand {
3434 if (T == usize) {
3535 return r.rng.get();
3636 } else {
37 var result: T = undefined;
38 r.fill_bytes(([]u8)((&result)[0...@sizeof(T)]));
39 return result;
37 var result: [@sizeof(T)]u8 = undefined;
38 r.fill_bytes(result);
39 return ([]T)(result)[0];
4040 }
4141 }
4242
......@@ -44,12 +44,12 @@ pub struct Rand {
4444 pub fn fill_bytes(r: &Rand, buf: []u8) {
4545 var bytes_left = buf.len;
4646 while (bytes_left >= @sizeof(usize)) {
47 *((&usize)(&buf[buf.len - bytes_left])) = r.scalar(usize);
47 ([]usize)(buf[buf.len - bytes_left...])[0] = r.rng.get();
4848 bytes_left -= @sizeof(usize);
4949 }
5050 if (bytes_left > 0) {
5151 var rand_val_array : [@sizeof(usize)]u8 = undefined;
52 ([]usize)(rand_val_array)[0] = r.scalar(usize);
52 ([]usize)(rand_val_array)[0] = r.rng.get();
5353 while (bytes_left > 0) {
5454 buf[buf.len - bytes_left] = rand_val_array[@sizeof(usize) - bytes_left];
5555 bytes_left -= 1;
......@@ -107,10 +107,8 @@ struct MersenneTwister(
107107 // TODO improve compile time eval code and then allow this function to be executed at compile time.
108108 #static_eval_enable(false)
109109 pub fn init(seed: int) -> Self {
110 var mt = Self {
111 .index = n,
112 .array = undefined,
113 };
110 var mt: Self = undefined;
111 mt.index = n;
114112
115113 var prev_value = seed;
116114 mt.array[0] = prev_value;