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

Method getrandbits

Lib/random.py:901–907  ·  view source on GitHub ↗

getrandbits(k) -> x. Generates an int with k random bits.

(self, k)

Source from the content-addressed store, hash-verified

899 return (int.from_bytes(_urandom(7)) >> 3) * RECIP_BPF
900
901 def getrandbits(self, k):
902 """getrandbits(k) -> x. Generates an int with k random bits."""
903 if k < 0:
904 raise ValueError('number of bits must be non-negative')
905 numbytes = (k + 7) // 8 # bits / 8 and rounded up
906 x = int.from_bytes(_urandom(numbytes))
907 return x >> (numbytes * 8 - k) # trim excess bits
908
909 def randbytes(self, n):
910 """Generate n random bytes."""

Callers 5

randbytesMethod · 0.45
uuid1Function · 0.45
uuid6Function · 0.45
uuid8Function · 0.45

Calls 1

from_bytesMethod · 0.45

Tested by

no test coverage detected