Skip to content

Commit a85e035

Browse files
David Rajchenbach-Tellerbrson
authored andcommitted
[Lib] rand.rs: type rng now defines next_float
1 parent eba88ef commit a85e035

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

src/lib/rand.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,21 @@ native "rust" mod rustrt {
1414
type rng =
1515
obj {
1616
fn next() -> u32;
17+
fn next_float() -> float;
1718
};
1819

1920
resource rand_res(c: rustrt::rctx) { rustrt::rand_free(c); }
2021

2122
fn mk_rng() -> rng {
2223
obj rt_rng(c: @rand_res) {
2324
fn next() -> u32 { ret rustrt::rand_next(**c); }
25+
fn next_float() -> float {
26+
let u1 = rustrt::rand_next(**c) as float;
27+
let u2 = rustrt::rand_next(**c) as float;
28+
let u3 = rustrt::rand_next(**c) as float;
29+
let scale = u32::max_value() as float;
30+
ret ((u1 / scale + u2) / scale + u3) / scale;
31+
}
2432
}
2533
ret rt_rng(@rand_res(rustrt::rand_new()));
2634
}

0 commit comments

Comments
 (0)