mirror of
https://github.com/RustPython/RustPython.git
synced 2026-10-28 18:59:04 +08:00
Add missing randbytes method on random
This commit is contained in:
1 parent
e17d20c920
commit
131c0b195d
2 files changed
+10
-1
No files matched your search
Vendored
+2
-1
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user