Skip to content

Commit af41564

Browse files
committed
libstd: add a method to generate random bytestrings.
1 parent 5d84652 commit af41564

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

src/libstd/rand.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,13 @@ type rng = obj {
3939
Return a random string composed of A-Z, a-z, 0-9.
4040
*/
4141
fn gen_str(len: uint) -> str;
42+
43+
/*
44+
Method: gen_bytes
45+
46+
Return a random byte string.
47+
*/
48+
fn gen_bytes(len: uint) -> [u8];
4249
};
4350

4451
resource rand_res(c: rustrt::rctx) { rustrt::rand_free(c); }
@@ -74,6 +81,16 @@ fn mk_rng() -> rng {
7481
}
7582
s
7683
}
84+
fn gen_bytes(len: uint) -> [u8] {
85+
let v = [];
86+
let i = 0u;
87+
while i < len {
88+
let n = rustrt::rand_next(**c) as uint;
89+
v += [(n % (u8::max_value as uint)) as u8];
90+
i += 1u;
91+
}
92+
v
93+
}
7794
}
7895
ret rt_rng(@rand_res(rustrt::rand_new()));
7996
}

0 commit comments

Comments
 (0)