Add missing randbytes method on random

This commit is contained in:
Osman Turan committed 2021-10-03 05:38:16 +03:00
1 parent e17d20c920
commit 131c0b195d
2 files changed
+10 -1

No files matched your search

+2 -1
View File
@@ -59,7 +59,7 @@ __all__ = ["Random","seed","random","uniform","randint","choice","sample",
"randrange","shuffle","normalvariate","lognormvariate",
"expovariate","vonmisesvariate","gammavariate","triangular",
"gauss","betavariate","paretovariate","weibullvariate",
"getstate","setstate", "getrandbits", "choices",
"getstate","setstate", "getrandbits", "randbytes", "choices",
"SystemRandom"]
NV_MAGICCONST = 4 * _exp(-0.5)/_sqrt(2.0)
@@ -775,6 +775,7 @@ weibullvariate = _inst.weibullvariate
getstate = _inst.getstate
setstate = _inst.setstate
getrandbits = _inst.getrandbits
randbytes = _inst.randbytes
if hasattr(_os, "fork"):
_os.register_at_fork(after_in_child=_inst.seed)
+8
View File
@@ -144,5 +144,13 @@ mod _random {
};
Ok(BigInt::from_biguint(sign, uint))
}
#[pymethod]
fn randbytes(&self, n: usize) -> PyResult<Vec<u8>> {
let mut buf = vec![0u8; n];
let mut rng = self.rng.lock();
rng.fill_bytes(&mut buf);
Ok(buf)
}
}
}