rust/src/libcore/rand.rs

134 lines
3.2 KiB
Rust
Raw Normal View History

2012-03-15 20:58:14 -05:00
#[doc = "Random number generation"];
export rng, extensions;
2012-02-01 04:45:23 -06:00
enum rctx {}
#[abi = "cdecl"]
native mod rustrt {
2012-02-01 04:45:23 -06:00
fn rand_new() -> *rctx;
fn rand_next(c: *rctx) -> u32;
fn rand_free(c: *rctx);
}
2012-03-07 20:17:30 -06:00
#[doc = "A random number generator"]
iface rng {
2012-03-07 20:17:30 -06:00
#[doc = "Return the next random integer"]
2011-10-26 18:24:31 -05:00
fn next() -> u32;
}
#[doc = "Extension methods for random number generators"]
impl extensions for rng {
2011-10-26 18:24:31 -05:00
#[doc = "Return a random float"]
fn gen_float() -> float {
let u1 = self.next() as float;
let u2 = self.next() as float;
let u3 = self.next() as float;
let scale = u32::max_value as float;
ret ((u1 / scale + u2) / scale + u3) / scale;
}
2012-03-07 20:17:30 -06:00
#[doc = "Return a random string composed of A-Z, a-z, 0-9."]
fn gen_str(len: uint) -> str {
let charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" +
"abcdefghijklmnopqrstuvwxyz" +
"0123456789";
let mut s = "";
let mut i = 0u;
while (i < len) {
let n = self.next() as uint % charset.len();
s = s + str::from_char(str::char_at(charset, n));
i += 1u;
}
s
}
2012-03-07 20:17:30 -06:00
#[doc = "Return a random byte string."]
fn gen_bytes(len: uint) -> [u8] {
let mut v = [];
let mut i = 0u;
while i < len {
let n = self.next() as uint;
v += [(n % (u8::max_value as uint)) as u8];
i += 1u;
}
v
}
}
2012-03-07 20:17:30 -06:00
#[doc = "Create a random number generator"]
2012-03-12 17:52:30 -05:00
fn rng() -> rng {
resource rand_res(c: *rctx) { rustrt::rand_free(c); }
impl of rng for @rand_res {
fn next() -> u32 { ret rustrt::rand_next(**self); }
}
@rand_res(rustrt::rand_new()) as rng
}
2012-01-17 21:05:07 -06:00
#[cfg(test)]
mod tests {
#[test]
fn test() {
let r1 = rand::rng();
2012-01-17 21:05:07 -06:00
log(debug, r1.next());
log(debug, r1.next());
{
2012-03-12 17:52:30 -05:00
let r2 = rand::rng();
2012-01-17 21:05:07 -06:00
log(debug, r1.next());
log(debug, r2.next());
log(debug, r1.next());
log(debug, r1.next());
log(debug, r2.next());
log(debug, r2.next());
log(debug, r1.next());
log(debug, r1.next());
log(debug, r1.next());
log(debug, r2.next());
log(debug, r2.next());
log(debug, r2.next());
}
log(debug, r1.next());
log(debug, r1.next());
}
#[test]
fn gen_float() {
let r = rand::rng();
let a = r.gen_float();
let b = r.gen_float();
log(debug, (a, b));
}
#[test]
fn gen_str() {
let r = rand::rng();
2012-01-17 21:05:07 -06:00
log(debug, r.gen_str(10u));
log(debug, r.gen_str(10u));
log(debug, r.gen_str(10u));
assert r.gen_str(0u).len() == 0u;
assert r.gen_str(10u).len() == 10u;
assert r.gen_str(16u).len() == 16u;
}
#[test]
fn gen_bytes() {
let r = rand::rng();
assert r.gen_bytes(0u).len() == 0u;
assert r.gen_bytes(10u).len() == 10u;
assert r.gen_bytes(16u).len() == 16u;
2012-01-17 21:05:07 -06:00
}
}
// Local Variables:
// mode: rust;
// fill-column: 78;
// indent-tabs-mode: nil
// c-basic-offset: 4
// buffer-file-coding-system: utf-8-unix
// End: