MCPcopy Index your code
hub / github.com/RustPython/RustPython / seed

Method seed

crates/stdlib/src/random.rs:48–67  ·  view source on GitHub ↗
(&self, n: OptionalOption<PyObjectRef>, vm: &VirtualMachine)

Source from the content-addressed store, hash-verified

46
47 #[pymethod]
48 fn seed(&self, n: OptionalOption<PyObjectRef>, vm: &VirtualMachine) -> PyResult<()> {
49 *self.rng.lock() = match n.flatten() {
50 Some(n) => {
51 // Fallback to using hash if object isn't Int-like.
52 let (_, mut key) = match n.downcast::<PyInt>() {
53 Ok(n) => n.as_bigint().abs(),
54 Err(obj) => BigInt::from(obj.hash(vm)?).abs(),
55 }
56 .to_u32_digits();
57 if cfg!(target_endian = "big") {
58 key.reverse();
59 }
60 let key = if key.is_empty() { &[0] } else { key.as_slice() };
61 MT19937::new_with_slice_seed(key)
62 }
63 None => MT19937::try_from_os_rng()
64 .map_err(|e| std::io::Error::from(e).to_pyexception(vm))?,
65 };
66 Ok(())
67 }
68
69 #[pymethod]
70 fn getrandbits(&self, k: PyObjectRef, vm: &VirtualMachine) -> PyResult<BigInt> {

Callers 1

initMethod · 0.45

Calls 9

absMethod · 0.80
as_bigintMethod · 0.80
lockMethod · 0.45
flattenMethod · 0.45
hashMethod · 0.45
reverseMethod · 0.45
is_emptyMethod · 0.45
as_sliceMethod · 0.45
to_pyexceptionMethod · 0.45

Tested by

no test coverage detected