[Lib] rand.rs: type rng now defines next_float
This commit is contained in:
parent
eba88ef3ea
commit
a85e035038
1 changed files with 8 additions and 0 deletions
|
@ -14,6 +14,7 @@ native "rust" mod rustrt {
|
|||
type rng =
|
||||
obj {
|
||||
fn next() -> u32;
|
||||
fn next_float() -> float;
|
||||
};
|
||||
|
||||
resource rand_res(c: rustrt::rctx) { rustrt::rand_free(c); }
|
||||
|
@ -21,6 +22,13 @@ resource rand_res(c: rustrt::rctx) { rustrt::rand_free(c); }
|
|||
fn mk_rng() -> rng {
|
||||
obj rt_rng(c: @rand_res) {
|
||||
fn next() -> u32 { ret rustrt::rand_next(**c); }
|
||||
fn next_float() -> float {
|
||||
let u1 = rustrt::rand_next(**c) as float;
|
||||
let u2 = rustrt::rand_next(**c) as float;
|
||||
let u3 = rustrt::rand_next(**c) as float;
|
||||
let scale = u32::max_value() as float;
|
||||
ret ((u1 / scale + u2) / scale + u3) / scale;
|
||||
}
|
||||
}
|
||||
ret rt_rng(@rand_res(rustrt::rand_new()));
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue