authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-02-05 04:30:19-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-02-05 04:30:19-07:00
logff5673ae1b74b8ad59ee23921e0bb4e25349d10f
treee6cb88dc691df220dc82a0c7bbe281f9b44d4390
parent15fe3c423562e984723bb56862062688422bf73b

add rand.float32 to standard library


1 files changed, 19 insertions(+), 0 deletions(-)

std/rand.zig+19
...@@ -53,6 +53,11 @@ pub struct Rand {...@@ -53,6 +53,11 @@ pub struct Rand {
53 }53 }
54 }54 }
5555
56 pub fn float32(r: &Rand) -> f32 {
57 const precision = 16777216;
58 return f32(r.range_u64(0, precision)) / precision;
59 }
60
56 fn generate_numbers(r: &Rand) {61 fn generate_numbers(r: &Rand) {
57 for (item, r.array, i) {62 for (item, r.array, i) {
58 const y : u32 = (item & 0x80000000) + (r.array[(i + 1) % ARRAY_SIZE] & 0x7fffffff);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,3 +96,17 @@ pub fn rand_new(seed: u32) -> Rand {
91 }96 }
92 return r;97 return r;
93}98}
99
100#attribute("test")
101fn 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}