| ... | @@ -34,9 +34,9 @@ pub struct Rand { | ... | @@ -34,9 +34,9 @@ pub struct Rand { |
| 34 | if (T == usize) { | 34 | if (T == usize) { |
| 35 | return r.rng.get(); | 35 | return r.rng.get(); |
| 36 | } else { | 36 | } else { |
| 37 | var result: T = undefined; | 37 | var result: [@sizeof(T)]u8 = undefined; |
| 38 | r.fill_bytes(([]u8)((&result)[0...@sizeof(T)])); | 38 | r.fill_bytes(result); |
| 39 | return result; | 39 | return ([]T)(result)[0]; |
| 40 | } | 40 | } |
| 41 | } | 41 | } |
| 42 | | 42 | |
| ... | @@ -44,12 +44,12 @@ pub struct Rand { | ... | @@ -44,12 +44,12 @@ pub struct Rand { |
| 44 | pub fn fill_bytes(r: &Rand, buf: []u8) { | 44 | pub fn fill_bytes(r: &Rand, buf: []u8) { |
| 45 | var bytes_left = buf.len; | 45 | var bytes_left = buf.len; |
| 46 | while (bytes_left >= @sizeof(usize)) { | 46 | 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(); |
| 48 | bytes_left -= @sizeof(usize); | 48 | bytes_left -= @sizeof(usize); |
| 49 | } | 49 | } |
| 50 | if (bytes_left > 0) { | 50 | if (bytes_left > 0) { |
| 51 | var rand_val_array : [@sizeof(usize)]u8 = undefined; | 51 | 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(); |
| 53 | while (bytes_left > 0) { | 53 | while (bytes_left > 0) { |
| 54 | buf[buf.len - bytes_left] = rand_val_array[@sizeof(usize) - bytes_left]; | 54 | buf[buf.len - bytes_left] = rand_val_array[@sizeof(usize) - bytes_left]; |
| 55 | bytes_left -= 1; | 55 | bytes_left -= 1; |
| ... | @@ -107,10 +107,8 @@ struct MersenneTwister( | ... | @@ -107,10 +107,8 @@ struct MersenneTwister( |
| 107 | // TODO improve compile time eval code and then allow this function to be executed at compile time. | 107 | // TODO improve compile time eval code and then allow this function to be executed at compile time. |
| 108 | #static_eval_enable(false) | 108 | #static_eval_enable(false) |
| 109 | pub fn init(seed: int) -> Self { | 109 | pub fn init(seed: int) -> Self { |
| 110 | var mt = Self { | 110 | var mt: Self = undefined; |
| 111 | .index = n, | 111 | mt.index = n; |
| 112 | .array = undefined, | | |
| 113 | }; | | |
| 114 | | 112 | |
| 115 | var prev_value = seed; | 113 | var prev_value = seed; |
| 116 | mt.array[0] = prev_value; | 114 | mt.array[0] = prev_value; |