rust/src/libstd/rand.rs

104 lines
2.2 KiB
Rust
Raw Normal View History

2011-10-26 18:24:31 -05:00
/*
Module: rand
2011-10-26 18:24:31 -05:00
Random number generation
*/
#[abi = "cdecl"]
native mod rustrt {
type rctx;
fn rand_new() -> rctx;
2011-07-27 07:19:39 -05:00
fn rand_next(c: rctx) -> u32;
fn rand_free(c: rctx);
}
2011-10-26 18:24:31 -05:00
/* Section: Types */
/*
Obj: rng
A random number generator
*/
iface rng {
2011-10-26 18:24:31 -05:00
/*
Method: next
Return the next random integer
*/
fn next() -> u32;
/*
Method: next_float
Return the next random float
*/
fn next_float() -> float;
/*
Method: gen_str
Return a random string composed of A-Z, a-z, 0-9.
*/
fn gen_str(len: uint) -> str;
/*
Method: gen_bytes
Return a random byte string.
*/
fn gen_bytes(len: uint) -> [u8];
}
resource rand_res(c: rustrt::rctx) { rustrt::rand_free(c); }
2011-10-26 18:24:31 -05:00
/* Section: Operations */
/*
Function: mk_rng
Create a random number generator
*/
fn mk_rng() -> rng {
impl of rng for @rand_res {
fn next() -> u32 { ret rustrt::rand_next(**self); }
fn next_float() -> float {
let u1 = rustrt::rand_next(**self) as float;
let u2 = rustrt::rand_next(**self) as float;
let u3 = rustrt::rand_next(**self) as float;
let scale = u32::max_value as float;
ret ((u1 / scale + u2) / scale + u3) / scale;
}
fn gen_str(len: uint) -> str {
let charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" +
"abcdefghijklmnopqrstuvwxyz" +
"0123456789";
let s = "";
let i = 0u;
while (i < len) {
let n = rustrt::rand_next(**self) as uint %
2011-11-08 23:27:48 -06:00
str::char_len(charset);
s = s + str::from_char(str::char_at(charset, n));
i += 1u;
}
s
}
fn gen_bytes(len: uint) -> [u8] {
let v = [];
let i = 0u;
while i < len {
let n = rustrt::rand_next(**self) as uint;
v += [(n % (u8::max_value as uint)) as u8];
i += 1u;
}
v
}
}
@rand_res(rustrt::rand_new()) as rng
}
// Local Variables:
// mode: rust;
// fill-column: 78;
// indent-tabs-mode: nil
// c-basic-offset: 4
// buffer-file-coding-system: utf-8-unix
// End: