| ... | ... | @@ -53,6 +53,11 @@ pub struct Rand { |
| 53 | 53 | } |
| 54 | 54 | } |
| 55 | 55 | |
| 56 | pub fn float32(r: &Rand) -> f32 { |
| 57 | const precision = 16777216; |
| 58 | return f32(r.range_u64(0, precision)) / precision; |
| 59 | } |
| 60 | |
| 56 | 61 | fn generate_numbers(r: &Rand) { |
| 57 | 62 | for (item, r.array, i) { |
| 58 | 63 | const y : u32 = (item & 0x80000000) + (r.array[(i + 1) % ARRAY_SIZE] & 0x7fffffff); |
| ... | ... | @@ -91,3 +96,17 @@ pub fn rand_new(seed: u32) -> Rand { |
| 91 | 96 | } |
| 92 | 97 | return r; |
| 93 | 98 | } |
| 99 | |
| 100 | #attribute("test") |
| 101 | fn test_float32() { |
| 102 | var r = rand_new(42); |
| 103 | |
| 104 | // TODO for loop with range |
| 105 | var i: i32 = 0; |
| 106 | while (i < 1000) { |
| 107 | const val = r.float32(); |
| 108 | if (!(val >= 0.0)) unreachable{}; |
| 109 | if (!(val < 1.0)) unreachable{}; |
| 110 | i += 1; |
| 111 | } |
| 112 | } |