2011-10-26 18:24:31 -05:00
|
|
|
/*
|
|
|
|
Module: rand
|
2011-06-15 13:19:50 -05:00
|
|
|
|
2011-10-26 18:24:31 -05:00
|
|
|
Random number generation
|
|
|
|
*/
|
2011-11-16 22:49:38 -06:00
|
|
|
#[abi = "cdecl"]
|
|
|
|
native mod rustrt {
|
2010-09-22 17:44:13 -05:00
|
|
|
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);
|
2010-07-25 23:45:09 -05:00
|
|
|
}
|
|
|
|
|
2011-10-26 18:24:31 -05:00
|
|
|
/* Section: Types */
|
|
|
|
|
|
|
|
/*
|
|
|
|
Obj: rng
|
|
|
|
|
|
|
|
A random number generator
|
|
|
|
*/
|
2012-01-11 05:54:39 -06:00
|
|
|
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;
|
2011-11-08 22:35:15 -06:00
|
|
|
|
|
|
|
/*
|
|
|
|
Method: gen_str
|
|
|
|
|
|
|
|
Return a random string composed of A-Z, a-z, 0-9.
|
|
|
|
*/
|
|
|
|
fn gen_str(len: uint) -> str;
|
2011-12-26 21:31:25 -06:00
|
|
|
|
|
|
|
/*
|
|
|
|
Method: gen_bytes
|
|
|
|
|
|
|
|
Return a random byte string.
|
|
|
|
*/
|
|
|
|
fn gen_bytes(len: uint) -> [u8];
|
2012-01-11 05:54:39 -06:00
|
|
|
}
|
2011-07-29 06:31:44 -05:00
|
|
|
|
2011-08-19 17:16:48 -05:00
|
|
|
resource rand_res(c: rustrt::rctx) { rustrt::rand_free(c); }
|
2010-07-25 23:45:09 -05:00
|
|
|
|
2011-10-26 18:24:31 -05:00
|
|
|
/* Section: Operations */
|
|
|
|
|
|
|
|
/*
|
|
|
|
Function: mk_rng
|
|
|
|
|
|
|
|
Create a random number generator
|
|
|
|
*/
|
2010-07-25 23:45:09 -05:00
|
|
|
fn mk_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";
|
|
|
|
let s = "";
|
|
|
|
let i = 0u;
|
|
|
|
while (i < len) {
|
2012-01-11 05:54:39 -06:00
|
|
|
let n = rustrt::rand_next(**self) as uint %
|
2011-11-08 23:27:48 -06:00
|
|
|
str::char_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] {
|
|
|
|
let v = [];
|
|
|
|
let i = 0u;
|
|
|
|
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
|
|
|
}
|
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:
|