2012-03-07 20:17:30 -06:00
|
|
|
#[doc = "Random number generation"]
|
2012-02-01 04:45:23 -06:00
|
|
|
|
|
|
|
enum rctx {}
|
|
|
|
|
2011-11-16 22:49:38 -06:00
|
|
|
#[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);
|
2010-07-25 23:45:09 -05:00
|
|
|
}
|
|
|
|
|
2012-03-07 20:17:30 -06:00
|
|
|
#[doc = "A random number generator"]
|
2012-01-11 05:54:39 -06:00
|
|
|
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;
|
|
|
|
|
2012-03-07 20:17:30 -06:00
|
|
|
#[doc = "Return the next random float"]
|
2011-10-26 18:24:31 -05:00
|
|
|
fn next_float() -> float;
|
2011-11-08 22:35:15 -06:00
|
|
|
|
2012-03-07 20:17:30 -06:00
|
|
|
#[doc = "Return a random string composed of A-Z, a-z, 0-9."]
|
2011-11-08 22:35:15 -06:00
|
|
|
fn gen_str(len: uint) -> str;
|
2011-12-26 21:31:25 -06:00
|
|
|
|
2012-03-07 20:17:30 -06:00
|
|
|
#[doc = "Return a random byte string."]
|
2011-12-26 21:31:25 -06:00
|
|
|
fn gen_bytes(len: uint) -> [u8];
|
2012-01-11 05:54:39 -06:00
|
|
|
}
|
2011-07-29 06:31:44 -05:00
|
|
|
|
2012-02-01 04:45:23 -06:00
|
|
|
resource rand_res(c: *rctx) { rustrt::rand_free(c); }
|
2010-07-25 23:45:09 -05:00
|
|
|
|
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 {
|
2012-01-11 05:54:39 -06:00
|
|
|
impl of rng for @rand_res {
|
|
|
|
fn next() -> u32 { ret rustrt::rand_next(**self); }
|
2011-10-17 07:44:29 -05:00
|
|
|
fn next_float() -> float {
|
2012-01-11 05:54:39 -06:00
|
|
|
let u1 = rustrt::rand_next(**self) as float;
|
|
|
|
let u2 = rustrt::rand_next(**self) as float;
|
|
|
|
let u3 = rustrt::rand_next(**self) as float;
|
2011-11-15 19:10:43 -06:00
|
|
|
let scale = u32::max_value as float;
|
2011-10-17 07:44:29 -05:00
|
|
|
ret ((u1 / scale + u2) / scale + u3) / scale;
|
|
|
|
}
|
2011-11-08 22:35:15 -06:00
|
|
|
fn gen_str(len: uint) -> str {
|
|
|
|
let charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" +
|
|
|
|
"abcdefghijklmnopqrstuvwxyz" +
|
|
|
|
"0123456789";
|
2012-03-12 22:04:27 -05:00
|
|
|
let mut s = "";
|
|
|
|
let mut i = 0u;
|
2011-11-08 22:35:15 -06:00
|
|
|
while (i < len) {
|
2012-01-11 05:54:39 -06:00
|
|
|
let n = rustrt::rand_next(**self) as uint %
|
2012-02-23 03:44:04 -06:00
|
|
|
str::len(charset);
|
2011-11-08 22:35:15 -06:00
|
|
|
s = s + str::from_char(str::char_at(charset, n));
|
|
|
|
i += 1u;
|
|
|
|
}
|
|
|
|
s
|
|
|
|
}
|
2011-12-26 21:31:25 -06:00
|
|
|
fn gen_bytes(len: uint) -> [u8] {
|
2012-03-12 22:04:27 -05:00
|
|
|
let mut v = [];
|
|
|
|
let mut i = 0u;
|
2011-12-26 21:31:25 -06:00
|
|
|
while i < len {
|
2012-01-11 05:54:39 -06:00
|
|
|
let n = rustrt::rand_next(**self) as uint;
|
2011-12-26 21:31:25 -06:00
|
|
|
v += [(n % (u8::max_value as uint)) as u8];
|
|
|
|
i += 1u;
|
|
|
|
}
|
|
|
|
v
|
|
|
|
}
|
2010-07-25 23:45:09 -05:00
|
|
|
}
|
2012-01-11 05:54:39 -06:00
|
|
|
@rand_res(rustrt::rand_new()) as rng
|
2010-07-25 23:45:09 -05:00
|
|
|
}
|
2012-01-17 21:05:07 -06:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test() {
|
2012-03-12 17:52:30 -05:00
|
|
|
let r1: rand::rng = 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 genstr() {
|
2012-03-12 17:52:30 -05:00
|
|
|
let r: rand::rng = 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));
|
2012-02-23 03:44:04 -06:00
|
|
|
assert(str::len(r.gen_str(10u)) == 10u);
|
|
|
|
assert(str::len(r.gen_str(16u)) == 16u);
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-09-22 17:44:13 -05:00
|
|
|
// Local Variables:
|
|
|
|
// mode: rust;
|
|
|
|
// fill-column: 78;
|
|
|
|
// indent-tabs-mode: nil
|
|
|
|
// c-basic-offset: 4
|
|
|
|
// buffer-file-coding-system: utf-8-unix
|
|
|
|
// End:
|