rand: add pub fn rand.read(mut buf []byte) and pub fn rand.bytes(needed int) ?[]byte{} + tests

This commit is contained in:
Delyan Angelov 2022-02-06 10:49:43 +02:00
parent ece73836aa
commit 66f21cae55
No known key found for this signature in database
GPG key ID: 66886C0F12D595ED
4 changed files with 82 additions and 0 deletions

View file

@ -138,3 +138,17 @@ fn init() {
default_rng = new_default()
C.atexit(deinit)
}
// read fills in `buf` a maximum of `buf.len` random bytes
pub fn read(mut buf []byte) {
p64 := unsafe { &u64(buf.data) }
u64s := buf.len / 8
for i in 0 .. u64s {
unsafe {
*(p64 + i) = default_rng.u64()
}
}
for i in u64s * 8 .. buf.len {
buf[i] = byte(default_rng.u32())
}
}